【问题标题】:Cannot use asynchronous methods in Cordova 3.4. Onsuccess is not called after the first call of plugin method不能在 Cordova 3.4 中使用异步方法。第一次调用插件方法后不调用onsuccess
【发布时间】:2014-05-29 13:11:33
【问题描述】:

以下代码无法正常工作:

public class TestPlugin extends CordovaPlugin {
   public static CallbackContext callbackContext;

   class TestRun implements Runnable {
      public void run() {
         try {
            Thread.sleep(10000);
         } catch (InterruptedException e) {}

         PluginResult result = new PluginResult(PluginResult.Status.OK, callbackContext.getCallbackId()); 
         result.setKeepCallback(false); 
         callbackContext.sendPluginResult(result); 
      }
   }

   @Override
   public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
      this.callbackContext = callbackContext;
      TestRun tr = new TestRun();
      new Thread(tr).start();
      return true;
   }
}

JS代码:

var Test = {
   getBTPrinters: function(successCallback, errorCallback) {
      cordova.exec(successCallback, errorCallback, "TestPlugin", "test", []);
      setTimeout(function(){ cordova.exec(successCallback, errorCallback, "TestPlugin", "test", []); }, 30000);
   }
}
当我第一次从 TestPlugin 调用方法时,不会调用我的 Javascript 代码中的

回调 onsuccess。
当我第二次从此插件调用方法时,我会从第一次调用中获得 onsuccess 回调。
在第三次调用插件方法后,我从第二次调用中获得了 onsuccess 回调。
等等等等。 它是 Cordova/Phonegap 的错误吗?
还是我用错了插件?

【问题讨论】:

  • 找到了解决方法,将其发布为答案。

标签: android cordova phonegap-plugins cordova-3 cordova-plugins


【解决方案1】:

不断调用 exec 将强制处理消息。

setInterval(function () {
    cordova.exec(null, null, '', '', [])
}, 200);

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2015-11-09
    相关资源
    最近更新 更多