我正在使用PhoneGap开发Android Web应用程序,它在大多数情况下运行良好,但有时当我想下载或上传文件时,应用程序挂起并且在任务完成之前不响应任何触摸事件。我将更详细地描述我编写的代码;
HTML code:
<a id="download">Download/a>
JavaScript代码:
$("#download").click(function() {
/*Here I used a plugin which is a Java class, and call this plugin and pass the required parameters*/
var data = [id, fileName];
window.download(callServiceSuccessCallBack,
callServiceFailCallBack, "download", data);
});
在此处注册插件的XML配置文件:
<plugins>
<plugin name="DowloadFilePlugin" value="com.example.plugin.DowloadFilePlugin" />
</plugins>
Java代码:
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
if ("download".equals(action)) {
// do something
}
return true;
}
当我按下按钮时,应用程序挂起,直到文件下载。
如果有人知道这个问题请告诉我如何解决,此外我还试过window.setTimeout(code, 0);
没有解决问题。
提前致谢!
答案 0 :(得分:0)
尝试使用延迟为0或100毫秒的setTimeout来开始您的操作:
$("#download").click(function() {
/*Here I used a plugin which is a Java class, and call this plugin and pass the required parameters*/
var data = [id, fileName];
setTimeout(function(){
window.download(callServiceSuccessCallBack,
callServiceFailCallBack, "download", data);
},0);
});
答案 1 :(得分:0)
我通过在JAVA代码中添加线程来解决问题,如下所示:
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
if ("beep".equals(action)) {
final long duration = args.getLong(0);
cordova.getThreadPool().execute(new Runnable() {
public void run() {
// do something....
}
});
return true;
}
return false;
}