繁体   English   中英

从phonegap应用程序调用插件方法时,从本机android函数返回的数据始终超出范围(未定义)

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

我无法弄清楚如何从cordova插件取回值到应用程序的javascript范围。 我可以向插件发送值。 我正在使用phonegap构建。 这是代码:

插件的javascript:

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

module.exports = {

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

插件的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"); }

该应用程序的javascript:

这很好用:

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

但是在这里,app_variable总是“未定义”:

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

我想在应用程序的javascript中使用data_needed,但它仅在回调主体的范围内定义。

我尝试过传递应用程序范围内的功能(及其变体):

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

但是“ app_variable”始终为“未定义”。 我究竟做错了什么?

在您的Java端尝试执行以下操作:

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

}
}   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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