2013-03-13 34 views
3

我创建了一个扩展CordovaPlugin的java类。如何从android中的javascript函数调用原生cordova插件?

对于例如,

public class SampleCardovaPlugin extends CordovaPlugin { 
    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if (action.equals("echo")) { 
      String message = args.getString(0); 
      this.echo(message, callbackContext); 
      return true; 
     } 
     return false; 
    } 
private void echo(String message, CallbackContext callbackContext) { 
    if (message != null && message.length() > 0) { 
     callbackContext.success(message); 
    } else { 
     callbackContext.error("Expected one non-empty string argument."); 
    } 
} 

}

我使用cordova2.5.0。

如何从我的javascript函数中调用这个插件? 请做必要的。

+0

参考https://stackoverflow.com/questions/35066122/cordova-plugin-javascript-function-call-from-native -in-IOS-4-0-0/44819500#44819500 – 2017-06-30 11:51:56

回答

2

您必须先将您的插件注册到res文件夹中的config.xml中。

然后在JavaScript:

cordova.exec(
    function(winParam) {}, 
    function(error) {}, 
    "service", 
    "action", 
    ["firstArgument", "secondArgument", 42, false]); 

所以你的情况

cordova.exec(
    function(data) { console.log(data);}, 
    function(error) { console.log(error);}, 
    "SampleCardovaPlugin", 
    "echo", 
    ["echo"]); 

您还必须确保该设备已准备就绪

看看http://docs.phonegap.com/en/2.0.0/guide_plugin-development_index.md.html

相关问题