0

Le plugin Java met à jour correctement un booléen dans TimezoneVariables à true/false si le fuseau horaire du périphérique change pendant que l'application est en arrière-plan. J'appelle mon plugin quand l'application reprend pour obtenir la valeur booléenne, et il affiche "getIsTimezoneChanged est vrai", mais il n'imprime pas "timezone did change".Cordova exec fonction de succès non appelée (Android)

La fonction javascript $ log.debug fonctionne correctement, tout comme la console.log. Le code relatif est ci-dessous si quelqu'un peut me dire pourquoi l'exec successcallback n'est pas appelé.

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

proxy Javascript:

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

Répondre

1

C'est parce que vous n'invoquez le rappel dans votre implémentation native. Cordova n'appelle pas automatiquement votre fonction de rappel de succès, vous devez utiliser le CallbackContext comme ceci:

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