简体   繁体   中英

when calling a plugin method from phonegap app, data returned from native android function is always out of scope (undefined)

I cannot figure out how to get a value back from a cordova plugin back to the app's javascript scope. I can send values to the plugin just fine. I'm using phonegap build. Here's the code:

the plugin's javascript:

 var exec = require('cordova/exec');

module.exports = {

getSomething: function(successCallback) { exec(successCallback,
null, "PluginName", "getSomething", []); } };

the plugin's java:

 import blah blah blah;

 public class PluginName extends CordovaPlugin { public PluginName(){ }

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

     if(action.equals("getSomething")) 
     {       this.getSomething(callbackContext); } } else { return false; }
     return true; }



   public void getSomething(CallbackContext callbackGetVol) { AndroidClass ac =
   (AndroidClass)
   this.cordova.getActivity().getSystemService(Context.SOME_SERVICE);

     int data_needed = ac.androidGetMethod(parameters);

     callbackGetVol.success(data_needed);    callbackGetVol.error("error"); }

The app's javascript:

This works great:

   PluginName.getSomething(function(data_needed) {Log("data = " + data_needed);}); 

But here, app_variable is always “undefined”:

 var app_variable;
 PluginName.getSomething(function(data_needed) {app_variable = data_needed;}); 

I would like to use data_needed in the app's javascript, but it's only defined within the confines of the callback's body.

I've tried passing a function that's in the app's scope (and variations of this):

  var foo = function() {
            var local_variable = 0;
            return {
                bar: function() {
                    PluginName.getSomething(function(local_variable){
                    return local_variable;});
                }
            };
        }();
        app_variable = foo.bar();

But “app_variable” is always “undefined”. What am I doing wrong?

Try with something like this on your java side:

public class MyPlugin extends CordovaPlugin {
    private static CallbackContext listenerCallbackContext;

public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
    Log.i(APP_TAG,"execute("+action+","+args+","+callbackContext+") starts");
 if(action.equals("getSomething")) 
 {       
       this.bindListener(callbackContext); 
 } else { return false; }
}

private boolean bindListener(final CallbackContext callbackContext){
    Log.i(APP_TAG,"listener(callbackContext) starts");
    cordova.getThreadPool().execute(new Runnable() {
        public void run(){
            try{
                listenerCallbackContext = callbackContext;
                ///////////////////////////////////
                //// FOR THE DEMO sendToJS() should be triggered when you get your result from JAVA
                //////////////////////////////////////  
                MyPlugin.sendToJS();
                ////////////////////////////
                ///////////////////
                /////////////////////// 
            }catch(Exception e){e.printStackTrace();}
        }
    });
    return true;
}

public static void sendToJS(){
    Log.i(APP_TAG, "sendToJS() starts");
    JSONObject eventData = new JSONObject();
    try {
        eventData.put("EVENT_DATA", "event_data");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, eventData);
    pluginResult.setKeepCallback(true);
    try{
            listenerCallbackContext.sendPluginResult(pluginResult);
    } catch(NullPointerException e){
        e.printStackTrace();
    }
    Log.i(APP_TAG, "sendQrCodeToJS() stops");

}
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM