Firefox android附加意图startActivity回调

时间:2016-02-15 16:17:46

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

我可以按照此处的说明从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意图?

1 个答案:

答案 0 :(得分:0)

如果您想使用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);
    }
}