cordova 插件封装
Init
plugman
- create
1
plugman create --name <pluginName> --plugin_id <pluginID> --plugin_version <version> [--path <directory>] [--variable NAME=VALUE]
- platform
1
plugman platform add --platform_name <platform>
- package.json
1
plugman createpackagejson <directory>
plugin.xml
- plugin name
1
2
3<js-module name="streamax" src="www/streamax.js">
<clobbers target="cordova.plugins.streamax" />
</js-module>1
2
3
4
5
6
7
8
9cordova.plugins.streamax.toast(
() => {
resolve()
},
() => {
reject()
},
[{ text }]
) - lib files
1
<lib-file src="src/android/libs/streamax.jar" />
- resource-file
1
<resource-file src="src/android/libs/armeabi" target="jniLibs/armeabi"/>
- source-file
1
<source-file src="src/android/xxxx.java" target-dir="src/com/packagename/www" />
JavaScript
register methods 注册插件函数
1 | function ObjectName() {} |
fireWindowEvent 触发window事件
1 | var channel = require("cordova/channel"); |
Android
gradle 使用gradle
- plugin.xml
1
2<framework src="src/android/libs/common.gradle" custom="true" type="gradleReference" />
- xxx.gradle
- 例子: 阿里云push
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25def minSdkVersion = 19
if(cdvMinSdkVersion == null) {
ext.cdvMinSdkVersion = minSdkVersion;
} else if (cdvMinSdkVersion.toInteger() < minSdkVersion) {
ext.cdvMinSdkVersion = minSdkVersion;
}
android {
defaultConfig {
applicationId "com.xxx.xxx" //包名
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64'
}
}
}
dependencies {
api 'com.aliyun.ams:alicloud-android-push:3.1.4@aar'
api 'com.aliyun.ams:alicloud-android-utils:1.1.3'
api 'com.aliyun.ams:alicloud-android-beacon:1.0.1'
api 'com.aliyun.ams:alicloud-android-ut:5.4.0'
api 'com.aliyun.ams:alicloud-android-push:3.1.4'
}
- 例子: 阿里云push
reflect methods 方法映射
- execute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
Class<T> classname = T.class;
try {
Method method = classname.getDeclaredMethod(action, JSONArray.class, CallbackContext.class);
return (Boolean) method.invoke(this, args, callbackContext);
} catch (NoSuchMethodException e) {
callbackContext.error("404");
} catch (InvocationTargetException e) {
callbackContext.error("500");
} catch (IllegalAccessException e) {
callbackContext.error("500");
} catch (Exception e) {
callbackContext.error("500");
}
return false;
} - 映射方法
1
2
3
4private boolean openMapLocation(JSONArray args, final CallbackContext callbackContext) throws JSONException {
JSONObject obj = args.getJSONObject(0);
Log.wtf(obj.toString())
} - 方法callback
1
2
3
4
5
6
7
8
9// 直接返回
callbackContext.error("转照片失败");
callbackContext.sucess("转照片失败");
// 多次触发
// 需要设置 setKeepCallback
PluginResult noResult = new PluginResult(PluginResult.Status.NO_RESULT);
noResult.setKeepCallback(true);
callbackContext.sendPluginResult(noResult); - 线程
1
2
3
4
5
6
7
8// 执行Java代码时候 需要和UI交互的用UI线程
cordova.getActivity().runOnUiThread(() -> { // lambda表达式
// 需要执行的代码
});
// 多线程
cordova.getThreadPool().execute(new Runnable() { // 正常写法
public void run() {}
}); - 引入依赖文件
1
2
3
4
5// aar 需要用gradle引入
<lib-file src="src/android/libs/nxminterface.aar" />
<lib-file src="src/android/libs/xnet-v1.0.0.jar" />
<resource-file src="src/android/libs/armeabi" target="jniLibs/armeabi"/>
<framework src="src/android/libs/bundle.gradle" custom="true" type="gradleReference" />