【问题标题】:Firefox android add-on intent startActivity callbackFirefox android 附加意图 startActivity 回调
【发布时间】:2016-02-15 16:17:46
【问题描述】:

我可以按照此处的说明从 Firefox 插件运行我的应用程序:https://github.com/cscott/skeleton-addon-fxandroid/blob/jni/bootstrap.js

我的示例代码是:

let jenv;
try {
    jenv = JNI.GetForThread();

    let GeckoAppShell = JNI.LoadClass(jenv, "org.mozilla.gecko.GeckoAppShell", {
                            static_methods: [
                                { name: "getContext", sig: "()Landroid/content/Context;" },
                            ],
                        });
    let Intent = JNI.LoadClass(jenv, "android.content.Intent", {
                     constructors: [
                         { name: "<init>", sig: "(Ljava/lang/String;)V" },
                     ],
                 });
    let Context = JNI.LoadClass(jenv, "android.content.Context", {
                      methods: [
                          { name: "startActivity", sig: "(Landroid/content/Intent;)V" },
                      ],
                  });

    let context = GeckoAppShell.getContext();
    let intent = Intent["new"]("my.example.app");
    context.startActivity(intent);

} finally {
    if (jenv) {
        JNI.UnloadClasses(jenv);
    }
}

JNI.jsm 中,我没有找到startActivityForResult() 的类似物。在我的应用程序中,我生成了一些数据。如何将其传递回我的插件?

我认为唯一的方法是按意图打开 Firefox。是否可以在我的插件中监听 Firefox 意图?

【问题讨论】:

  • 不工作怎么办?此代码是否会导致 Firefox 崩溃?或者您只是在贴花 startActivityForResult 时遇到问题?

标签: android firefox-addon firefox-addon-sdk


【解决方案1】:

如果你想使用startActivityForResult,你必须获得壁虎的主要活动。此代码未经测试,但请检查一下:

function genMethodSIG(aParamsArr, aRet) {
  // aParamsArr is an array of SIG's for each param. Not fully qualified name, meaning if its a class, it needs the surrouning L and ;
  // aRet is a SIG for the return value. Not fully qualified name, same meaning as above row

  return '(' + (aParamsArr ? aParamsArr.join('') : '') + ')' + aRet;
}

function fullyQualifiedNameOfClass(aClass) {
    // aClass is a string with L and ; arround it
    return aClass.substr(1, aClass - 2);
}

// (SIG\.[a-z]*?)\.substr\(1, SIG\.[a-z]*?\.length - 2\) // fullyQualifiedNameOfClass\($1\)

var my_jenv = null;
try {
    my_jenv = JNI.GetForThread();

    //////////////// start - declares
    var SIG = {
        Activity: 'Landroid/app/Activity;', // java.lang.Object -> android.content.Context -> android.content.ContextWrapper -> android.view.ContextThemeWrapper -> android.app.Activity // http://developer.android.com/reference/android/app/Activity.html
        Context: 'Landroid/content/Context;', // java.lang.Object -> android.content.Context // http://developer.android.com/reference/android/content/Context.html
        GeckoAppShell: 'Lorg/mozilla/gecko/GeckoAppShell;', // https://dxr.mozilla.org/mozilla-central/source/mobile/android/base/Makefile.in#296
        GeckoInterface: 'Lorg/mozilla/gecko/GeckoAppShell$GeckoInterface;', // https://dxr.mozilla.org/mozilla-central/source/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java#125-131
        Intent: 'Landroid/content/Intent;', // java.lang.Object -> android.content.Intent // http://developer.android.com/reference/android/content/Intent.html
        String: 'Ljava/lang/String;',
        int: 'I',
        void: 'V'
    };

    var Activity = JNI.LoadClass(my_jenv, fullyQualifiedNameOfClass(SIG.Activity), {
        methods: [
            {
                /* http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29
                 * public void startActivityForResult (Intent intent, int requestCode)
                 */
                name: 'startActivityForResult',
                sig: genMethodSIG([
                        SIG.Intent, // intent
                        SIG.int     // requestCode
                    ],
                    SIG.void        // return
                )
            }
        ]
    });

    var Context = JNI.LoadClass(my_jenv, fullyQualifiedNameOfClass(SIG.Context), {
        methods: [
            {
                /* http://developer.android.com/reference/android/content/Context.html#startActivity%28android.content.Intent%29
                 * public abstract void startActivity (Intent intent)
                 */
                name: 'startActivity',
                sig: genMethodSIG([
                        SIG.Intent  // intent
                    ],
                    SIG.void        // return
                )
            }
        ]
    });

    var GeckoAppShell = JNI.LoadClass(my_jenv, fullyQualifiedNameOfClass(SIG.GeckoAppShell), {
        static_methods: [
            {
                /* https://dxr.mozilla.org/mozilla-central/source/mobile/android/base/java/org/mozilla/gecko/GeckoAppShell.java#2144
                 * public static Context getContext()
                 */
                name: 'getContext',
                sig: genMethodSIG(
                    null,           // no parameters
                    SIG.Context     // return
                )
            },
            {
                /* https://dxr.mozilla.org/mozilla-central/source/mobile/android/base/java/org/mozilla/gecko/GeckoAppShell.java#2199
                 * public static GeckoInterface getGeckoInterface()
                 */
                name: 'getGeckoInterface',
                sig: genMethodSIG(
                    null,                   // no parameters
                    SIG.GeckoInterface      // return
                )
            }
        ]
    });

    var GeckoInterface = JNI.LoadClass(my_jenv, fullyQualifiedNameOfClass(SIG.GeckoInterface), {
        methods: [
            {
                /* https://dxr.mozilla.org/mozilla-central/source/mobile/android/base/java/org/mozilla/gecko/BaseGeckoInterface.java#54
                 * public Activity getActivity()
                 */
                name: 'getActivity',
                sig: genMethodSIG(
                    null,                   // no parameters
                    SIG.Activity        // return
                )
            }
        ]
    });

    var Intent = JNI.LoadClass(my_jenv, fullyQualifiedNameOfClass(SIG.Intent), {
        constructors: [ // i think all constructors return void and have name '<init>'
            {
                /* http://developer.android.com/reference/android/content/Intent.html#Intent%28java.lang.String%29
                 * public Intent (String action)
                 */
                // name: '<init>', // not necessary
                sig: genMethodSIG([
                        SIG.String  // action
                    ],
                    SIG.void        // return
                )
            }
        ],
        methods: [
            {
                /* http://developer.android.com/reference/android/content/Context.html#startActivity%28android.content.Intent%29
                 * public abstract void startActivity (Intent intent)
                 */
                name: 'startActivity',
                sig: genMethodSIG([
                        SIG.Intent  // intent
                    ],
                    SIG.void        // return
                )
            }
        ]
    });

    //////////////// end - declares

    //////////////// start - my code

    // var cContext = GeckoAppShell.getContext();


    var cGeckoInterface = GeckoAppShell.getGeckoInterface();
    var cGeckoMainActivity = cGeckoInterface.getActivity();

    var cIntent = Intent['new']('my.example.app'); // i dont know your intent action string, its up to you

    var cRequestCode = 1; // i dont know what code you want to use here, its up to you
    cGeckoMainActivity.startActivityForResult(cIntent, cRequestCode);

    //////////////// end - my code
} finally {
    if (my_jenv) {
        JNI.UnloadClasses(my_jenv);
    }
}

【讨论】:

  • @umni4ek 请跟随这里来学习如何使用onActivityResult 我也被卡住了,所以问JNI.jsm的作者-github.com/cscott/intent-addon/issues/10
  • 是的,我没有找到如何覆盖 onactivityresult 的方法,我会关注这个问题,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
相关资源
最近更新 更多