7

我正在为 Android cordova/phonegap 应用程序创建一个自定义插件,并且本机 Java 端启动了一个活动,其中包括由它启动的服务调用的回调。这个想法是回调每隔一秒左右就会受到服务的影响并且效果很好,但问题是我似乎无法在另一个线程中运行它,因此主cordova线程被阻塞并且应用程序完全没有响应。

根据我正在这样做的文档:

@Override
public boolean execute(String action, JSONArray args, final CallbackContext context) throws JSONException
{

/* snip */
    cordova.getThreadPool().execute(new Runnable()
    {
        public void run()
        {
            Intent myIntent = new Intent(this.cordova.getActivity(), myMonitoring.class);
            this.cordova.getActivity().startActivity(myIntent);    
            callbackContext.success();
        }
    });

    return true;
}

我在这里意识到 JS 回调永远不会被调用(即callbackContext.success();),因为活动被阻塞,但实际的 phonegap 线程不应该继续运行return true吗?如果我删除startActivity呼叫,则应用程序将按预期继续工作。

4

1 回答 1

6

I'm investigating a problem (another). But it seems that UI Main thread is not equal to Cordova thread. And what happens is that the UI Main thread is indeed released, but the Cordova thread is not.

BTW, I also think that there is a difference between:

callbackContext.success(); 

and

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));

It is still not baked, sorry. If I'll find some more, I'll update.

Also, you can call callbackContext.success(); first, and then the rest.. (if it helps in anything).

于 2014-03-14T12:27:26.483 回答