【问题标题】:Cordova PhoneGap Android Callback with dataCordova PhoneGap Android 回调与数据
【发布时间】:2015-03-16 00:59:26
【问题描述】:

我不知道如何将数据从 JAVA 获取回 javascript,因此我可以在 Android 上使用 Cordova 更新 UI。这里有两个按钮,当它们被按下时,它们会增加我想要返回 UI 并更新屏幕的位置计数器,但无法弄清楚它是如何返回的。

我以为我可以将 switchPos 添加到回调中,但似乎无法在 javascript 中的任何地方获取它,我打算使用“.innerHTML =”语句将值写入 UI。

这里是插件 javascript。

cordova.define(‘myplugin', function (require, exports, module) {
   module.exports = {

    switch1: function (success, failure) {
    cordova.exec(success, failure, “MyPlugin", “switch1”, []);
    },

    switch2: function (success, failure) {
    cordova.exec(success, failure, “MyPlugin", “switch2”, []);
    }


  };
});

这是调用它们的 javascript。

 var myplugin = cordova.require('myplugin');

 var myapp = {

appButton1: function(){
     myplugin.switch1();
},

appButton2: function(){
     myplugin.switch2();
}

};

这是插件的 JAVA 代码。

public class MyPlugin extends CordovaPlugin {

// actions
private static final String SWITCH1 = “switch”1;
private static final String SWITCH2 = “switch”2;

// callbacks
private CallbackContext connectCallback;

// Switch Counters
public static int switchPos1 = 1;
public static int switchPos2 = 1;


@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

    boolean validAction = true;

    if (action.equals(SWITCH1)) {
    Log.d("Valid Action = SWITCH1 Pos: “ + switchPos1);
    switchPos1++;
    callbackContext.success(switchPos1);

} else if (action.equals(SWITCH2)) {
    Log.d("Valid Action = SWITCH2 Pos: “ + switchPos2);
    switchPos2++;
    callbackContext.success(switchPos2);

} else { 
    validAction = false;
}

return validAction;

}

}

【问题讨论】:

    标签: java javascript cordova phonegap-plugins cordova-plugins


    【解决方案1】:

    当你调用插件时,你必须传递成功和失败函数:

    var myplugin = cordova.require('myplugin');
    
     var myapp = {
    
    appButton1: function(){
         myplugin.switch1(function(data){alert(data);},function(error){alert(error);});
    },
    
    appButton2: function(){
         myplugin.switch2(function(data){alert(data);},function(error){alert(error);});
    }
    
    };
    

    【讨论】:

      【解决方案2】:

      你可以试试这个

      首先,您需要在 config.xml 中声明您的自定义插件。您可以在 res > xml 文件夹中找到此文件。

      <feature name="CustomPlugin">
            <param name="android-package" value="com.Phonegap.CustomPlugin" />
      </feature>
      

      那你需要用Java-code来实现插件

      public class CustomPlugin extends CordovaPlugin {
      
          @Override
          public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
                  throws JSONException {
      
              if (action.equals("sayHello")){
                  try {
                      String responseText = "Hello world, " + args.getString(0);
                      callbackContext.success(responseText);
                  } catch (JSONException e){
                      callbackContext.error("Failed to parse parameters");
                  }
                  return true;
              }
      
              return false;
          }
      }
      

      最后我们从 javascript 调用插件

      function initial(){
          var name = $("#NameInput").val();
          cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
      }
      
      function sayHelloSuccess(data){
          alert("OK: " + data);
      }
      
      function sayHelloFailure(data){
          alert("FAIL: " + data);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-23
        • 2014-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多