科尔多瓦3.4不能使用异步方法。的onSuccess插件方法的第一个电话后不叫 [英] Cannot use asynchronous methods in Cordova 3.4. Onsuccess is not called after the first call of plugin method
问题描述
继code不能正常工作:
Following code does not work properly:
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 code:
JS code:
var Test = {
getBTPrinters: function(successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "TestPlugin", "test", []);
setTimeout(function(){ cordova.exec(successCallback, errorCallback, "TestPlugin", "test", []); }, 30000);
}
}
回调的onSuccess 时,我打电话从TestPlugin首次的方法我的JavaScript code不叫。结果
当我打电话的方法,从这个插件第二次,然后我得到第一次调用的onSuccess回调。结果
插件的方法的第三个电话后,我获得了第二个电话的onSuccess回调。结果
等等等等。
它是科尔多瓦/ PhoneGap的一个错误?结果
或者,我用我的插件以错误的方式?
Callback onsuccess in my Javascript code is not called when I call a method from TestPlugin for the first time.
When I call method from this plugin for the second time, then I get onsuccess callback from the first call.
After third call of plugin's method I get onsuccess callback from the second call.
And so on and so forth.
Is it a bug of Cordova/Phonegap?
Or I use my plugin in a wrong way?
推荐答案
不断地调用exec将强制进程的消息。
Constantly calling exec will force process the messages.
setInterval(function () {
cordova.exec(null, null, '', '', [])
}, 200);
这篇关于科尔多瓦3.4不能使用异步方法。的onSuccess插件方法的第一个电话后不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!