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

的-什么线程用于Cordova插件回调?,第1张

概述CallbackContext的方法应该在哪个线程中调用?CordovaPlugin#execute(…)的文档说它是在WebView线程中调用的.和UI线程一样吗?如果是这样,那可能就是我的答案.如果WebView线程不是UI线程,而我应该在WebView线程中回叫,是否可以异步进行回调?解决方法:我把您放在android插件文档的Thre

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:

@OverrIDepublic 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:

@OverrIDepublic 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中.对该列表的所有访问均已同步

总结

以上是内存溢出为你收集整理的的-什么线程用于Cordova插件回调?全部内容,希望文章能够帮你解决的-什么线程用于Cordova插件回调?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1079017.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存