Get return value from PhoneGap plugin

I built a very simple plugin PhoneGapto start testing how I am going to create some of my own actions on Android.

JavaScript:

 function callNativePlugin() {
            cordova.exec(nativePluginResultHandler, nativePluginErrorHandler, "Database", "saveAdvertencia", [ 1, "TesteAdv" ]);
        }

    function nativePluginResultHandler(result) {
        alert("SUCCESS: \r\n" + result);
    }

    function nativePluginErrorHandler(error) {
        alert("ERROR: \r\n" + error);
    }

Java:

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("saveAdvertencia")) {
        advertenciaDS = new AdvertenciaDS(cordova.getActivity());
        callbackContext.sendPluginResult(new PluginResult(Status.OK, new JSONArray("test")));
        return true;
    }
    return false;
}

I need a way to get the result from an action in the same method. It is very difficult to always deal with 3 ways (1 to complete step 1 to determine what the success action will be. 1 to determine what the error action will be.) When you are not really in control when they are called, because it PhoneGapcalls them upon completion of an action

If I need to get some data from the Android Native database:

  • Call "cordova.exec" in JavaScript.
  • Phonegap will call your plugin.
  • 2 : PhoneGap, , . , sucessfull .
  • . , 2 , , , PhoneGap . ?
+5
3

, , :

function callNativePlugin(callback) {
   cordova.exec(function(result) {
       callback(null, result);
   }, function(result) {
       callback("myerror");
   }, "Database", "saveAdvertencia", [ 1, "TesteAdv" ])
};

node.js, , - :

//define the callback function
var callbackFunction = function(err, result) {
    if (err) {
        console.log("there was an error");
    } else {
        console.log("success", result);
    }
};

//call the plugin
callNativePlugin(callbackFunction);

, API- cordova, .

+5

cordova.exec() , , , . ( - , .)

, , , - ( http://htmlpresenter.com/api.html):

var Database = {
  saveAdvertencia: function(args, callback) {
    cordova.exec(callback,
      function(error) { console.log("Error calling Database.saveAdvertencia: " + error); },
      "Database", "saveAdvertencia", args)
  }
}

, , :

Database.Advertencia([1, "TesteAdv"], function(result) { alert(result); });

, ( , ), , :

function saveAdvertencia() {
  log = function(message) { console.log(message) };
  cordova.exec(log, log, "Database", "saveAdvertencia", arguments);
}

saveAdvertencia(1, "TesteAdv");

(Edit:) , , :

+2

Inside JavaScript, I was able to get the value of the PhoneGap plugin with just one easily manageable recursive method:

function callNativePlugin(result, error, service, action, json) {
    if (result != null) {
        //alert("SUCCESS: \r\n" + result);
        return result;
    } else if (error != null) {
        //alert("ERROR: \r\n" + error);
        return error;
    }
    return cordova.exec(function(result) {callNativePlugin(result, null, null, null);}, function(error) {callNativePlugin(null, error, null, null);}, service, action, json);
}
0
source

All Articles