Chicowitz
Chicowitz

Reputation: 5939

Cordova exec success function not called (Android)

The Java plugin successfully updates a boolean in TimezoneVariables to true/false if the device's timezone changes while the app is in the background. I call my plugin when the app resumes to get the boolean value, and it prints "getIsTimezoneChanged is true", but it does not print "timezone did change".

The $log.debug javascript function works fine, just like console.log. The relevent code is below if anyone can tell me why the exec successcallback isn't being called.

Java code:

@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;
}

Javascript proxy:

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 code:

  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);

Upvotes: 1

Views: 1444

Answers (1)

David
David

Reputation: 7507

Thats because you never invoke the callback in your native implementation. Cordova does not automatically call your success callback function, you have to this using the CallbackContext like this:

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;
}

Upvotes: 2

Related Questions