Javascript 定制Android Cordova插件
我正在尝试为Cordova应用程序创建一个定制的Android插件。我似乎缺少了一些没有调用的Javascript 定制Android Cordova插件,javascript,android,cordova,Javascript,Android,Cordova,我正在尝试为Cordova应用程序创建一个定制的Android插件。我似乎缺少了一些没有调用的androidalert.alert() 创建插件: plugman create --name AndroidAlert --plugin_id cordova-androidalert --plugin_version 0.0.1` plugman createpackagejson AndroidAlert 将源文件添加到src/android/AndroidAlert.java: packag
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>
添加插件:
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
尝试将Chrome DevTools连接到Webview以检查错误并添加断点,并使用Android Studio对Java执行同样的操作(通过打开
平台/Android
)。@DaveAlden当我在平台/Android
目录中打开Android项目时,我得到一个“无法确定Android SDK目录”。知道为什么吗?
cordova emulate android