1.定义一个class 继承自 cordovaplugin
2.res/xml/config.xml 定义插件节点
<feature name="QRCode">
<param name="android-package" value="so.laodao.plugin.QRCode" />
</feature>
参数说明:name 插件名称,cordova.exec(fn,fn,插件名称,'方法',参数)
3.调用
cordova.exec(success,error,xml中定义的插件名称,要调用的插件的方法名称,参数)
1.
public class QRCode extends CordovaPlugin { private static final String TAG = "QRCode_Plugin"; private static final String ACTION_SCAN="scan"; private static final String ACTION_ENCODE="encode"; @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (ACTION_SCAN.equalsIgnoreCase(action)) { scan(args, callbackContext); return true; } if (ACTION_ENCODE.equalsIgnoreCase(action)) { scan(args, callbackContext); return true; } return false; } public void scan(JSONArray args, CallbackContext callbackContext) { callbackContext.success(); } }
2.
<feature name="QRCode"> <param name="android-package" value="so.laodao.plugin.QRCode" /> </feature>
3.
document.addEventListener('deviceready', function(){ cordova.exec(success,error,'QRCode','scan',[]); function success(){ alert('success'); } function error(){ alert('error'); } }, false);