一、创建空白的cordova项目
1、到你想创建项目的目录下,打开黑窗口
cordova create hellocom.example.helloHelloWorld
hello:项目目录名
com.exmple.hello:项目包名
HelloWorld:应用的名字
2、cd到刚才创建的目录hello下,添加android平台
cd hello
cordova platform add android
<?xml version='1.0' encoding='utf-8'?>
<plugin id="<span style="background-color: rgb(255, 204, 204);">cordova-myPlugin-pluginb</span>" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name><span style="background-color: rgb(255, 204, 204);">PluginB</span></name>
<js-module name="PluginB" src="www/PluginB.js">
<clobbers target="cordova.plugins.PluginB" />
</js-module>
</plugin>
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-myPlugin-pluginb" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>PluginB</name>
<js-module name="PluginB" src="www/PluginB.js">
<clobbers target="cordova.plugins.PluginB" />
</js-module>
<span style="background-color: rgb(204, 204, 204);"> <!--针对android平台的配置-->
<platform name="android">
<em><!--将插件src/android目录下的PluginB复制到项目的src/com/plugins/pluginb目录下--></em>
<source-file src="src/android/PluginB.java" target-dir="<strong>src/com/plugins/pluginb</strong>" />
<em><!--这里是将feature这个标签队里面的内容复制到config.xml中--></em>
<config-file target="res/xml/config.xml" parent="/*">
<feature name="PluginB"></span>
<span style="background-color: rgb(204, 204, 204);"> <!--value:com.plugins.pluginb 插件的包名,pluginB 插件java文件名,插件的包名和插件的id不是一回事,虽然后有时候默认用插件id做包名-->
<param name="android-package" value="<strong>com.plugins.pluginb.PluginB</strong>" />
<param name="onload" value="true" />
</feature>
</config-file></span>
</platform>
</plugin>
public class PluginB extends CordovaPlugin {
private Context context;
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
//获取context
context = this.cordova.getActivity().getApplicationContext();
}
@Override
public boolean execute(String action, final CordovaArgs args,
CallbackContext callbackContext) throws JSONException {
//
if ("save".equals(action)) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
String key = args.optString(0);
String value = args.optString(1);
saveData(key, value);
}
});
} else if ("get".equals(action)) {
String key = args.optString(0);
String values = this.getString(context, key, "");
callbackContext.success(values);
}
return true;
}
private void saveData(String key, String value) {
this.putString(context, key, value);
}
public static final String FILE_NAME = "share_data";
public static SharedPreferences sp;
/**
* 每次使用工具类的时候,首先判断是不是已经有sp对象了,如果有了就不用再次创建了。
*
* @param context
* @return
*/
private static SharedPreferences getPreferences(Context context) {
if (sp == null) {
sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
}
return sp;
}
/**
* 保存key value值
*
* @param context
* @param key
* @param value
*/
public static void putString(Context context, String key, String value) {
SharedPreferences sp = getPreferences(context);
sp.edit().putString(key, value).commit();
}
/**
* 根据key值获取value
*
* @param context
* @param key
* @param defValue
* @return
*/
public static String getString(Context context, String key, String defValue) {
String value = getPreferences(context).getString(key, defValue);
return value;
}
}
<span style="color: rgb(255, 0, 0);">cordova.define("cordova-myplugin-pluginb.PluginB", function(require, exports, module) {</span>
var exec = require('cordova/exec');
var pluginb= {
save:function() {
uccess, error, "PluginB", "save", [key,value]
},
get:function() {
exec(null, null, "PluginB", "get", []);
}
};
module.exports = pluginb;
<span style="color: rgb(255, 0, 0);">}); </span>
<span style="color:#ff0000;">cordova.define("cordova-myplugin-pluginb.PluginB", function(require, exports, module) {</span>
var exec = require('cordova/exec');
var pluginb= {
save:function() {
uccess, error, "PluginB", "save", [key,value]
},
get:function() {
exec(null, null, "PluginB", "get", []);
}
};
module.exports = pluginb;
<span style="color:#ff0000;">}); </span>
这样插件就写完了,如果想测试可以在index.html