的-什么线程用于Cordova插件回调?

CallbackContext的方法应该在哪个线程中调用?

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

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

解决方法:

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

Threading

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;
}

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/en/3.5.0/guide_platforms_android_plugin.md.html#Android%20Plugins

凯文注:

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

上一篇:javascript-移动设备上的Cordova中的3D WebGL


下一篇:使用Cordova在Android中记录并获取音量以生成波形