我写了一个Java类,它是cordova插件的一部分,主要代码是:
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;
}
}
这是插件的js代码:
cordova.define("cordova-plugin-NAME.PLUGINNAME", function(require, exports, module) {
module.exports = {
getdata: function(message, successCallback) {
cordova.exec(successCallback, null, "ClassName", "getData", [message]);
}
};
});
这是我用来调用插件函数的js:
function myFunc(){
alert('Function started');
ClassName.getdata(successCallback, null);
}
document.addEventListener('DOMContentLoaded', function(){
document.getElementById('test').addEventListener('click', myFunc);
});
我有两个问题:
1)我的java类输出一个String结果,如何将结果传递给我的javascript函数(myfunc())?
2)我不明白" successCallback"的成功是什么?有人可以举个例子吗?
答案 0 :(得分:1)
sucessCallback应如下所示:
successCallback:function(event){
.... Do things with the 'event' object received from JAVA
}
“event”对象是从JAVA回答的对象。