科尔多瓦插件回调使用什么线程? [英] What thread to use for Cordova plugin callback?

查看:40
本文介绍了科尔多瓦插件回调使用什么线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CallbackContext ?

CordovaPlugin#execute(...)的文档说它是在WebView线程中调用的.和UI线程一样吗?如果是这样,那可能就是我的答案.

The documentation for CordovaPlugin#execute(...) says it's called in the WebView thread. Is that the same as the UI thread? If so, then that's probably my answer.

如果WebView线程不是UI线程,而我应该在WebView线程中回叫,是否可以异步进行操作?

If the WebView thread is not the UI thread, and I'm supposed to call back in the WebView thread, is it possible to do so asynchronously?

推荐答案

我把android插件文档的Threading部分放了. 插件都是异步的,当您调用它们时,会收到成功或失败的回调.如果原生任务太长,thead就是为了不阻塞UI.

I put you the Threading section of the android plugins documentation. The plugins are all asynch, when you call them you get a success or failure callback. The theads are just to not block the UI if the native task is too long.

线程

插件的JavaScript无法在WebView的主线程中运行 界面;相反,它像执行一样在WebCore线程上运行 方法.如果需要与用户界面进行交互,则应 使用以下变体:

The plugin's JavaScript does not run in the main thread of the WebView interface; instead, it runs on the WebCore thread, as does the execute method. If you need to interact with the user interface, you should use the following variation:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        final long duration = args.getLong(0);
        cordova.getActivity().runOnUiThread(new Runnable() {
            public void run() {
                ...
                callbackContext.success(); // Thread-safe.
            }
        });
        return true;
    }
    return false;
}

如果不需要在主界面上运行,请使用以下内容 线程,但也不想阻塞WebCore线程:

Use the following if you do not need to run on the main interface's thread, but do not want to block the WebCore thread either:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if ("beep".equals(action)) {
        final long duration = args.getLong(0);
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                ...
                callbackContext.success(); // Thread-safe.
            }
        });
        return true;
    }
    return false;
}

http://docs.phonegap.com/zh/3.5.0/guide_platforms_android_plugin.md.html#Android%20插件

凯文的注解:

调用CallbackContext的方法最终将调用CordovaWebView#sendPluginResult(PluginResult cr, String callbackId).在CordovaWebViewImpl中该方法的实现调用NativeToJsMessageQueue#addPluginResult(cr, callbackId),最终导致将元素添加到同步块内的LinkedList中.对List的所有访问均已同步

Calls to the methods of CallbackContext end up calling CordovaWebView#sendPluginResult(PluginResult cr, String callbackId). The implementation of that method in CordovaWebViewImpl calls NativeToJsMessageQueue#addPluginResult(cr, callbackId), which ultimately results in an element being added to a LinkedList inside a synchronized block. All accesses to that List are synchronized

这篇关于科尔多瓦插件回调使用什么线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆