Android Phonegap:当 AsyncTask 完成时通知 javascript [英] Android Phonegap: Notify javascript when an AsyncTask is finished

查看:23
本文介绍了Android Phonegap:当 AsyncTask 完成时通知 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当用户单击 webview 中的按钮时,将调用 phonegap 插件以触发异步任务以从 Internet 下载文件.现在我想在异步任务完成时将信号发送回 javascript 部分.但我不知道该怎么做,因为我的插件在异步任务完成之前已经发回了一些东西.有谁知道我如何在没有 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 Google Group 中问过这个问题,这是Simon Mac Donald 的回复.它非常适合我:

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

您可以通过使用插件 API 轻松处理这种情况.它在核心 API 项 Connection 和 Battery 中实现.你什么需要做的是:

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

正如我所说,您可以查看 GitHub 中的代码,了解我们如何将其用于连接和电池.

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

这篇关于Android Phonegap:当 AsyncTask 完成时通知 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆