Android的PhoneGap的:当一个AsyncTask的完成通知的JavaScript通知、PhoneGap、Android、JavaScript

2023-09-13 23:40:10 作者:百事可爱

在我的应用程序,当在web视图按钮用户点击,一个PhoneGap的插件会被调用来触发的AsyncTask从互联网上下载文件。现在,我希望在AsyncTask的完成发送信号回JavaScript的一部分。但我不知道该怎么做,因为我的插件已经寄东西回来的AsyncTask完成之前。有谁知道我可以不用插件在PhoneGap的通知我的JavaScript的一部分?

in my app, when user click on a button in webview, a phonegap plugin will be called to trigger an asynctask to download file from internet. Now i want to send a signal back to javascript part when the asynctask is finished. But i don't know how to do it, because my plugin had already send something back before the asynctask is finished. Does anyone know how i can notify my javascript part without plugin in Phonegap?

推荐答案

我也问PhoneGap的谷歌集团这个问题,这里是西蒙·麦克唐纳的回应。它完美对我来说:

I also asked this question in Phonegap Google Group, here is response of Simon Mac Donald. It works perfectly for me:

您可以通过使用插件API很容易地处理这种情况。它 在核心API项目连接和电池实现。什么,你 需要做的是:

You can handle this situation by using the Plugin API quite easily. It is implemented in the core API items Connection and Battery. What you need to do is:

1)在你的插件中的execute()方法保存你的callbackId。

1) In your execute() method of your plugin save the callbackId you get.

2)返回一个NO_RESULT插件的结果,并设置保持回调ID为true。

2) Return a NO_RESULT plugin result and set keep callback id to true.

    PluginResult pluginResult = new  PluginResult(PluginResult.Status.NO_RESULT); 
    pluginResult.setKeepCallback(true); 
    return pluginResult; 

3)当你异步Java方法完成后返回另一个插件的结果是这样的:

3) When you async java method finishes return another plugin result like this:

    PluginResult result = new PluginResult(PluginResult.Status.OK, data); 
    result.setKeepCallback(false); 
    this.success(result, this.myCallbackId); 

就像我说的,你可以看看code在GitHub上,看看我们是如何使用这种用于连接和电池。

As I said, you can look at the code in GitHub to see how we are using this for Connection and Battery.