简体   繁体   English

如何从Android中的Cordova插件访问CordovaActivity appView?

[英]How to get access to CordovaActivity appView from cordova plugin in android?

I have created a cordova plugin for android which handles the received push notification. 我为Android创建了cordova插件,用于处理收到的推送通知。 When the push received, I'd like to send a javascript to the current webView of the cordova app like below: 收到推送后,我想将javascript发送到cordova应用程序的当前webView,如下所示:

appView.sendJavascript("some javascript");

But the problem is from the activity created as cordova plugin, I cannot get access to the appView of current webView in cordova app. 但是问题出在作为科尔多瓦插件创建的活动中,我无法在科尔多瓦应用程序中访问当前WebView的appView。

I'm really appreciate it if anyone can guide me how to access to the current webView of cordova pp. 如果有人可以指导我如何访问cordova pp当前的webView,我将非常感激。

You can use reflection. 您可以使用反射。

Code answer: 代码答案:

Put the below static field and initialiser under your class: 将以下静态字段和初始化程序放在您的课​​程下:

private static Field appViewField;
static {
    try {
        Class<?> cdvActivityClass = CordovaActivity.class;
        Field wvField = cdvActivityClass.getDeclaredField("appView");
        wvField.setAccessible(true);
        appViewField = wvField;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
}

Then, under the "execute" method: 然后,在“执行”方法下:

try {
    final CordovaWebView webView = (CordovaWebView) appViewField.get(cordova.getActivity());
    Handler mainHandler = new Handler(cordova.getActivity().getMainLooper());
    final Looper myLooper = Looper.myLooper();
    mainHandler.post(new Runnable() {
        @Override
        public void run() {
            // Finally do whatever you want with 'appView', for example:
            webView.clearCache();
            new Handler(myLooper).post(new Runnable() {
                @Override
                public void run() {
                    callbackContext.success();
                }
            });
        }
    });

} catch (Throwable e) {
    callbackContext.error(e.getMessage());
}

Details: 细节:

Using reflection, we store the protected appView field declaration of CordovaActivity.class in our plugin class, to be accessed later when needed. 使用反射,我们将appViewprotected appView字段声明CordovaActivity.class在我们的插件类中,以便以后需要时进行访问。

Since the field is not accessible to us because it's protected , we must manually set it as accessible by calling setAccessible(true); 由于该字段由于protected而无法访问,因此我们必须通过调用setAccessible(true);将其手动设置为可访问setAccessible(true);

The reason we use use a different thread rather our plugin's thread which is called "JavaBridge", is that the appView field is stored on the thread of MainLooper , thus we must access it using the MainLooper , and when we're finished with it, call cordova's callbackContext success/error methods only under the "JavaBridge" thread again. 我们使用另一个线程而不是插件的线程“ JavaBridge”的原因是, appView字段存储在MainLooper的线程上,因此我们必须使用MainLooper访问,完成后,仅在“ JavaBridge”线程下再次调用cordova的callbackContext成功/错误方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM