Javascript 如何在Cordova插件中调用函数
我编写了一个简单的cordova插件,它显示一个警报。 JS文件:alert.JSJavascript 如何在Cordova插件中调用函数,javascript,cordova,callback,cordova-plugins,Javascript,Cordova,Callback,Cordova Plugins,我编写了一个简单的cordova插件,它显示一个警报。 JS文件:alert.JS module.exports = { alert: function(title, message, buttonLabel, successCallback) { cordova.exec(successCallback, null, // No failure callback "Alert", "alert",
module.exports = {
alert: function(title, message, buttonLabel, successCallback) {
cordova.exec(successCallback,
null, // No failure callback
"Alert",
"alert",
[title, message, buttonLabel]);
}
};
Java文件:Alert.Java
package com.acme.plugin.alert;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Alert extends CordovaPlugin {
protected void pluginInitialize() {
}
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException {
if (action.equals("alert")) {
alert(args.getString(0), args.getString(1), args.getString(2), callbackContext);
return true;
}
return false;
}
private synchronized void alert(final String title,
final String message,
final String buttonLabel,
final CallbackContext callbackContext) {
new AlertDialog.Builder(cordova.getActivity())
.setTitle(title)
.setMessage(message)
.setCancelable(false)
.setNeutralButton(buttonLabel, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
}
})
.create()
.show();
}
}
如何从另一个js调用alert.js的alert函数?我应该传递什么参数来映射到successCallback???根据cordova git创建插件,您可以这样做 在需要调用插件功能的地方添加以下代码:
cordova.plugins.<PluginName>.<method>();
插件..();
其中,
是您的插件名称,
是您的方法。查看此链接-您的alert.js文件已经调用了插件代码,因此我不确定您为什么需要其他js文件来调用alert.js共享您的plugin.xml
会有所帮助。