PhoneGap的1.0.0 - 的Andr​​oid 2.1 / 2.3 - 不能够作出PhoneGap的插件异步工作 [英] PhoneGap 1.0.0 - Android 2.1/2.3 - Not able to make a phonegap plugin to work asynchronously

查看:183
本文介绍了PhoneGap的1.0.0 - 的Andr​​oid 2.1 / 2.3 - 不能够作出PhoneGap的插件异步工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的:


  1. 用户填写表单数据,点击提交。

  2. 用户$ P $的加载屏幕psented。 (HTML元素)。

  3. 应用程序使异步调用,PhoneGap的插件,它在数据库保存这些数据。这就是问题的所在,因为调用是同步的,而不是。

  4. 当HTML应用程序接收到一个回调,我隐藏加载屏幕。

由于同步调用的,这就是我的了:

Because of synchronous call that's what I got:


  1. 用户填写表单,提交

  2. HTML应用程序死机,数据被保存到数据库中。

  3. 出现加载屏幕

  4. 回调被调用时,在几毫秒后。

下面是一些演示(修剪)code:

Here's some demo (trimmed) code:

Java的:

public class SomePlugin extends Plugin
...
public PluginResult execute(String action, JSONArray data, String callbackId) 
{
    PluginResult result = null;
    //
    // save data in the background...
    //
    Log.d("TAG", "Some Message...");

    result = new PluginResult(Status.OK, "");
    // or 
    // result = new PluginResult(Status.ERROR);

    return result;
}
...
public boolean isSynch(String action) {
     return false; // always do async...
}

JavaScript的:

JavaScript:

$('#loading-screen').show();

var successCallback = function() {
    console.log('Success Callback');
    $('#loading-screen').hide();
};
var failureCallback = function() {
    console.log('Failed Callback');
    $('#loading-screen').hide();
};

PhoneGap.exec(successCallback, failureCallback, 'PluginName', 'actionName', data);

从PhoneGap的来源:

From PhoneGap source:

* Execute a PhoneGap command.  It is up to the native side whether this action is synch or async.
* The native side can return:
*      Synchronous: PluginResult object as a JSON string
*      Asynchrounous: Empty string ""
* If async, the native side will PhoneGap.callbackSuccess or PhoneGap.callbackError,
* depending upon the result of the action.

所以,我想,也许这条线是不正确在这种情况下:

So I thought maybe this line is incorrect in that case:

new PluginResult(Status.OK, "");

注:如果包(JavaScript的)PhoneGap.exec呼叫的setTimeout(1秒例如延迟),加载屏幕会工作正常(它仍然冻结,但用户有一个即时反馈),但是这显然不是溶液

Note: If wrap (JavaScript) PhoneGap.exec call in setTimeout (with a delay of 1 sec for example), loading screen will work "properly" (it's still frozen but user have an instant feedback), but that's obviously not a solution.

我想我只是没有看到明显的东西在这里,只有一个参数或一些地方。

I think I just not seeing something obvious here, just one parameter or something somewhere.

感谢。

推荐答案

我认为它完美的的的AsyncTask
刚刚处理您在doInBackground dbStorage和处理onPostExcecute整理()。
你可以自由更新onProgressUpdate状态

I think its perfect for an AsyncTask just process you dbStorage in doInBackground and handle finishing in onPostExcecute(). You are free to update status in onProgressUpdate

这篇关于PhoneGap的1.0.0 - 的Andr​​oid 2.1 / 2.3 - 不能够作出PhoneGap的插件异步工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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