Android Phonegap: Notify javascript when an AsyncTask is finished

后端 未结 3 827
礼貌的吻别
礼貌的吻别 2020-12-02 19:19

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

3条回答
  •  星月不相逢
    2020-12-02 19:36

    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.


提交回复
热议问题