之前是有道记录的,而且没有很好的整理,先移过来,后面完善
针对cordova安卓插件的开发,这里给出例子,实现震动和提示两个小功能
plugin.xml内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?xml version="1.0" encoding="UTF-8"?> <plugin xmlns ="http://apache.org/cordova/ns/plugins/1.0" id ="com.xc.sep" version ="0.0.1" > <name > Sep</name > <js-module src ="www/Sep.js" name ="Sep" > <clobbers target ="Sep" /> </js-module > <platform name ="android" > <source-file src ="src/android/Sep.java" target-dir ="src/com/xc/sep" /> <config-file target ="res/xml/config.xml" parent ="/*" > <feature name ="Sep" > <param name ="android-package" value ="com.xc.sep.Sep" /> </feature > </config-file > <config-file target ="AndroidManifest.xml" parent ="/*" > <uses-permission android:name ="android.permission.VIBRATE" /> </config-file > </platform > </plugin >
Sep.js内容 1 2 3 4 5 6 7 8 9 10 11 12 13 var exec = require ('cordova/exec' );var Sep = function ( ) {};Sep.prototype.showVibration = function (success, error ) { exec(success, error, 'Sep' , 'showVibration' , []); }; Sep.prototype.showToast=function (content,type ) { exec(null , null , "Sep" , "showToast" , [content,type]); }; var sep = new Sep();module .exports = sep;
Sep.java内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.xc.sep;import org.apache.cordova.CallbackContext;import org.apache.cordova.CordovaInterface;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.CordovaWebView;import org.json.JSONArray;import org.json.JSONException;import android.content.Context;import android.media.AudioManager;import android.os.Vibrator;public class Sep extends CordovaPlugin { public void initialize (CordovaInterface cordova, CordovaWebView webView) { super .initialize(cordova, webView); Context context = this .cordova.getActivity().getApplicationContext(); } @Override public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("showVibration" .equals(action)) { showVibration(); callbackContext.success("success" ); return true ; }else if ("showToast" .equals(action)) { showToast(args.getString(0 ), args.getInt(1 )); callbackContext.success("success" ); return true ; } callbackContext.error("error 没有该方法" ); return false ; } private void showVibration () { CordovaInterface cordova = this .cordova; AudioManager manager = (AudioManager) this .cordova.getActivity().getSystemService(Context.AUDIO_SERVICE); if (manager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) { Vibrator vibrator = (Vibrator) this .cordova.getActivity().getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000 ); } } private void showToast (String text,int type) { CordovaInterface cordova = this .cordova; android.widget.Toast.makeText(cordova.getActivity(), text, type).show(); } }
安装插件 add后面是你的插件文件夹路径(本地或github上等)
1 cordova plugin add C:\Users\Administrator\Desktop\Sep
js中调用 1 2 3 4 5 6 7 8 Sep.showVibration(function (message ) { alert("showVibration :" +message); }, function (message ) { alert("showVibration :" +message); }); Sep.showToast("测试提示2秒" ,2 );
在java调用js的两种方式 1 2 3 PluginResult result = new PluginResult(PluginResult.Status.OK,返回内容); result.setKeepCallback(true ); callback.sendPluginResult(result);
1 2 3 4 5 6 7 8 9 10 String format = "cordova.plugins.Easemob.onMessageReadAckReceivedInAndroidCallback(%s);" ; JSONArray jSONArray = new JSONArray(); packageJSONArray(arg0,jSONArray); final String js = String.format(format, jSONArray);cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run () { webView.loadUrl("javascript:" + js); } });