Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Phonegap: Notify javascript when an AsyncTask is finished

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?

like image 999
Peacemoon Avatar asked Oct 13 '11 08:10

Peacemoon


1 Answers

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


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) In your execute() method of your plugin save the callbackId you get.

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

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


like image 72
Peacemoon Avatar answered Sep 30 '22 18:09

Peacemoon