未调用 Cordova exec 成功函数 (Android)

Cordova exec success function not called (Android)

如果设备的时区在应用程序处于后台时更改,Java 插件会成功将 TimezoneVariables 中的布尔值更新为 true/false。当应用程序恢复获取布尔值时,我调用我的插件,它打印 "getIsTimezoneChanged is true",但不打印 "timezone did change".

$log.debug javascript 函数工作正常,就像 console.log 一样。如果有人能告诉我为什么没有调用 exec successcallback,相关代码如下。

Java代码:

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
    if(action.equals("createTimezoneChangeListener")) {
        TimezoneVariables.setCallbackContext(callbackContext);
        TimezoneVariables.setIsTimezoneChanged(false);
    }else if(action.equals("checkTimezoneChange")){
        if (TimezoneVariables.getIsTimezoneChanged()){
            Log.d("TimezoneUpdater","getIsTimezoneChanged is true");
            TimezoneVariables.setIsTimezoneChanged(false);
            return true;
        } else return false;        
    }
    return true;
}

Java脚本代理:

function TimezoneUpdater()
{

}

TimezoneUpdater.prototype = {
    checkTimezoneChange: function (changedCallback) {
        cordova.exec(changedCallback, null, "TimezoneUpdater", "checkTimezoneChange", []);
    }
}

module.exports = {
    createTimezoneChangeListener: function(){       
        cordova.exec(null, null, "TimezoneUpdater", "createTimezoneChangeListener", []);        
        return new TimezoneUpdater();
    }
};

www javascript 代码:

  var ref = window.timezoneUpdater.createTimezoneChangeListener();
  document.addEventListener("resume", function(){
    ref.checkTimezoneChange(function(){
      //Timezone did change while app was in the background
      $log.debug("timezone did change");
    });
  }, false);

那是因为您从不在本机实现中调用回调。 Cordova 不会自动调用您的成功回调函数,您必须像这样使用 CallbackContext

if (TimezoneVariables.getIsTimezoneChanged()) {
  callbackContext.success("timezone changed"); // you can pass whatever you want here
  return true;
else {
  callbackContext.error("timezone did not change"); // you have to pass an error callback for that too
  return false;
}