我正在尝试为Cordova应用程序创建一个自定义的Android插件。我似乎错过了一些没有调用androidalert.alert()
创建插件:
plugman create --name AndroidAlert --plugin_id cordova-androidalert --plugin_version 0.0.1`
plugman createpackagejson AndroidAlert
将源文件添加到src / android / AndroidAlert.java:
package com.example.plugin;
import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
public class AndroidAlert extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if ("alert".equals(action)) {
callbackContext.success();
return true;
}
return false; // Returning false results in a "MethodNotFound" error.
}
}
将类和方法添加到www / AndroidAlert.js:
var exec = require('cordova/exec');
exports.alert = function(arg0, success, error) {
exec(success, error, "AndroidAlert", "alert", [arg0]);
};
添加plugin.xml平台并编辑clobbers:
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-androidalert" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>AndroidAlert</name>
<js-module name="AndroidAlert" src="www/AndroidAlert.js">
<clobbers target="androidalert" />
</js-module>
<platform name="android">
<config-file target="config.xml" parent="/*">
<feature name="Alert">
<param name="android-package" value="com.androidalert.Alert"/>
</feature>
</config-file>
<source-file src="src/android/AndroidAlert.java" target-dir="src/com/androidalert/" />
</platform>
</plugin>
添加android平台:
cordova platform add android
添加插件:
cordova plugin add ../AndroidAlert
在www / js / index.js中添加到Cordova应用程序:
onDeviceReady: function() {
this.receivedEvent('deviceready');
//navigator.notification.alert("Water", function() {}, "ERROR", "Okay");
var success = function(message) {
alert(message);
}
var failure = function() {
alert("Error calling Hello Plugin");
}
androidalert.alert("World", success, failure);
}
在模拟器中运行:
cordova emulate android