user6309010
user6309010

Reputation:

How i can get return value from cordova plugin in Android?

I wrote a Java class that is a part of a cordova plugin, the main code is:

public class ClassName extends CordovaPlugin {

  protected void pluginInitialize() {}

  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    if (action.equals("getData")) {
      CallbackContext callback = null;

      Test ts  = new Test();
      String result = ts.TestNow();

      PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
      pluginResult.setKeepCallback(true);
      callbackContext.sendPluginResult(pluginResult);

      return true;
    }
    return false;
  }
}

This is the js code of the plugin:

cordova.define("cordova-plugin-NAME.PLUGINNAME", function(require, exports, module) {
module.exports = {
  getdata: function(message, successCallback) {
    cordova.exec(successCallback, null, "ClassName", "getData", [message]);
  }
};
});

And this is the js that i use to call plugin function:

function myFunc(){
 alert('Function started');
 ClassName.getdata(successCallback, null);
 }

document.addEventListener('DOMContentLoaded', function(){
    document.getElementById('test').addEventListener('click', myFunc);
});

I have two questions:

1) My java class get in output a String result, how I can pass the result to my javascript function ( myfunc() )?

2) I don't understand what is the fucntion of "successCallback", could someone make me one example ?

Upvotes: 1

Views: 3178

Answers (1)

nyluje
nyluje

Reputation: 3773

successCallback in JS is executed when retrieving callbackContext.sendPluginResult(pluginResult) from JAVA.

The sucessCallback should look like:

successCallback:function(event){ 
    .... Do things with the 'event' object received from JAVA
}

The "event" object is the object answered from JAVA.

Upvotes: 1

Related Questions