- 從調用的角度看流程
前端調用(clobbers)——>cordova_plugins.js(clobbers對應插件id和插件文件所在的路徑)—–>js部分(配置着插件的名字,已經插件里面都有的方法)——>config.xml(根據插件的名字找到對應的插件原生文件的包名)——>原生(根據匹配到的方法名,來調用原生方法,另外也可以獲取到js傳遞下來的參數)
簡單說:前端調用——>橋梁:(cordova_plugin.js clobbers)—->js文件——>橋梁:(config.xml 插件名)—–>原生
- 關鍵1:前端接口:cordova.plugins.QtPlugin 是插件的clobbers,在cordova_plugins.js里面配置,get是插件提供的方法之一,在插件的js文件里面有
function GetToken(key, success, failed){
document.addEventListener('deviceready', function(){
cordova.plugins.QtPlugin.get(key,
success,
failed
);
}, false);
}
- 關鍵2:cordova_plugins.js:配置插件clobbers,已經對應的插件id和插件js文件的路徑
"file": "plugins/com.qt.cordova.plugin/www/qtplugin.js",
"id": "com.qt.cordova.plugin.QtPlugin",
"clobbers": [
"cordova.plugins.QtPlugin"
]
- 關鍵3:js部分,這里是插件的id
cordova.define("com.qt.cordova.plugin.QtPlugin", function(require, exports, module) {
- 關鍵4:js部分,這里是插件的名字,以及插件的方法
QtPlugin.prototype.save = function(key,value,success, error){
exec(success, error, "QtPlugin", "save", [key,value]);
};
QtPlugin.prototype.get = function(key,success, error){
exec(success, error, "QtPlugin", "get", [key]);
};
- 關鍵5:config.xml:插件名對應的原生代碼的包名
<feature name="BarcodeScanner" >
<param
name="android-package"
value="com.phonegap.plugins.barcodescanner.BarcodeScanner" />
</feature>
- 關鍵6:原生代碼:匹配到方法,調用原生方法saveDate()
if ("save".equals(action)) {
System.out.println("save被調用");
cordova.getThreadPool().execute(new Runnable() {
public void run() {
String key = args.optString(0);
String value = args.optString(1);
saveDate(key, value);
}
});
}
關於cordova插件要注意5個部分
第一部分:原生代碼部分:匹配js方法,調用需要的原生方法
public boolean execute(String action, final CordovaArgs args,
final CallbackContext callbackContext) throws JSONException {
//save方法,給前端調用用來保存key value形式的鍵值對
if ("save".equals(action)) {
System.out.println("save被調用");
cordova.getThreadPool().execute(new Runnable() {
public void run() {
String key = args.optString(0);
String value = args.optString(1);
saveDate(key, value);
}
});
}
//get方法,給前端調用用來獲取指定key對應的value值
else if ("get".equals(action)) {
//get會被調用兩次,一次獲取用戶名,另外一次獲取密碼
System.out.println("get被調用");
String key = args.optString(0);
System.out.println("get被調用key:" + key);
String values = PrefUtils.getString(MyApplication.ctx, key, "");// 從sp取
System.out.println("返回的values:" + values);
callbackContext.success(values);
} else if ("getLocalVersion".equals(action)) {
JSONObject json = new JSONObject();
String localVer = DeviceUtil.getVersionName(context);
if (!localVer.equals("")) {
json.put("localVer", localVer);
json.put("platform", "android");
callbackContext.success(json);
return true;
} else {
json.put("code", 107);
json.put("msg", "獲取本地版本號失敗");
callbackContext.error(json);
}
}
2、config.xml:插件名和插件名對應的原生代碼文件
<feature name="Whitelist" >
<param
name="android-package"
value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param
name="onload"
value="true" />
</feature>
<feature name="BarcodeScanner" >
<param
name="android-package"
value="com.phonegap.plugins.barcodescanner.BarcodeScanner" />
</feature>
<!-- QtPlugin是插件名字,com.plugins.qt.QtPlugin是插件原生代碼的文件包名-->
<feature name="QtPlugin" >
<param
name="android-package"
value="com.plugins.qt.QtPlugin" />
</feature>
<feature name="InAppBrowser">
<param name="android-package" value="org.apache.cordova.inappbrowser.InAppBrowser" />
</feature>
<preference name="Splashscreen" value="screen" />
<preference name="SplashScreenDelay" value="15000" />
<feature name="SplashScreen">
<param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
<param name="onload" value="true" />
</feature>
<!-- app在pause后不會停止代碼-->
<preference name="KeepRunning" value="false"/>
<!-- app在pause后不會停止代碼-->
<preference name="KeepRunning" value="false"/>
第三部分:cordova_plugins.js, 插件id和插件的js文件對應,另外clobbers是前端調用的時候使用的,這樣就相當於前端調用clobbers,然后就會找到對應的js文件
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
{
"file": "plugins/cordova-plugin-whitelist/whitelist.js",
"id": "cordova-plugin-whitelist.whitelist",
"runs": true
},
{
"file": "plugins/phonegap-plugin-barcodescanner/www/barcodescanner.js",
"id": "phonegap-plugin-barcodescanner.BarcodeScanner",
"clobbers": [
"cordova.plugins.barcodeScanner"
]
},
{
"file": "plugins/cordova-plugin-splashscreen/www/splashscreen.js",
"id": "cordova-plugin-splashscreen.SplashScreen",
"clobbers": [
"navigator.splashscreen"
]
},
{
"file": "plugins/com.qt.cordova.plugin/www/qtplugin.js",
"id": "com.qt.cordova.plugin.QtPlugin",
"clobbers": [
"cordova.plugins.QtPlugin"
]
},
{
"id": "cordova-plugin-inappbrowser.inappbrowser",
"file": "plugins/cordova-plugin-inappbrowser/www/inappbrowser.js",
"pluginId": "cordova-plugin-inappbrowser",
"clobbers": [
"cordova.InAppBrowser.open",
"window.open"
]
}
];
module.exports.metadata =
// TOP OF METADATA
{
"cordova-plugin-whitelist": "1.2.1",
"phonegap-plugin-barcodescanner": "4.1.0",
"cordova-plugin-splashscreen": "3.1.0",
"com.qt.cordova.plugin":"1.0.0",
"cordova-plugin-inappbrowser": "1.6.1"
};
// BOTTOM OF METADATA
});
第四部分 js文件部分
<!-- com.qt.cordova.plugin.QtPlugin 這是插件的id,在cordova_plugins.js里面配置了-->
cordova.define("com.qt.cordova.plugin.QtPlugin", function(require, exports, module) {
var exec = require('cordova/exec');
function QtPlugin(){};
//一、消息通知
QtPlugin.prototype.noticeAlert = function(enable){
exec(null,null,"QtPlugin", "noticeAlert", [enable]);
};
QtPlugin.prototype.noticeSound = function(enable){
exec(null,null,"QtPlugin", "noticeSound",[enable]);
};
QtPlugin.prototype.noticeVibrate = function(enable){
exec(null,null,"QtPlugin", "noticeVibrate", [enable]);
};
QtPlugin.prototype.getAlertStatus = function(success){
exec(success,null,"QtPlugin", "getAlertStatus", []);
};
//二、保存數據
QtPlugin.prototype.save = function(key,value,success, error){
exec(success, error, "QtPlugin", "save", [key,value]);
};
QtPlugin.prototype.get = function(key,success, error){
exec(success, error, "QtPlugin", "get", [key]);
};
//三、更新
QtPlugin.prototype.getLocalVersion = function(success,error){
exec(success,error,"CheckUpdate","getLocalVersion",[]);
};
QtPlugin.prototype.updateH5 = function(success,error){
alert("updateH5");
exec(success,error,"QtPlugin","updateH5",[]);
};
QtPlugin.prototype.update = function(url,error){
exec(null,error, "QtPlugin","update", [url]);
};
//四、UserInfo
QtPlugin.prototype.setUser = function(userInfo){
exec(null,null,"QtPlugin", "setUser", [userInfo]);
};
QtPlugin.prototype.getUser = function(success){
exec(success,null,"QtPlugin", "getUser", []);
};
QtPlugin.prototype.getDeviceInfo = function(success){
exec(success,null,"QtPlugin", "getDeviceInfo", []);
};
//五、網大
QtPlugin.prototype.wdStudy = function(studyInfo,success, error){
exec(success, error, "QtPlugin", "wdStudy", [studyInfo]);
};
var qtPlugin = new QtPlugin();
module.exports = qtPlugin;
});
第五部分前端調用
// key 是字符串, success成功回調,有一個參數,參數為key對應的value字符串,failed為失敗回調
function GetToken(key, success, failed){
document.addEventListener('deviceready', function(){
cordova.plugins.QtPlugin.get(key,
success,
failed
);
}, false);
}
/*調用閃屏插件*/
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.splashscreen.hide();
}
/*網大學習app接口 */
function WdStudy(studyInfo, success, failed){
document.addEventListener('deviceready', function(){
cordova.plugins.QtPlugin.wdStudy(studyInfo,
function(date){
success(date);
},
function(error){
failed(error.code,error.msg);
}
);
}, false);
}
前端開發使用接口:在index.js里面引入接口文件后,直接用window.方法就可以
代碼中調用:
window.WdStudy && window.WdStudy(studyInfo, () => {
}, () => {
});
- 舉例:
- 提供的接口
function goThirdSite(url, start, stop, error){
document.addEventListener('deviceready', function(){
if(!cordova.InAppBrowser){
error(0)
}else {
var inAppBrowserRef =
cordova.InAppBrowser.open(url, '_blank', 'location=no');
inAppBrowserRef.addEventListener('loadstart', start);
inAppBrowserRef.addEventListener('loadstop', stop);
inAppBrowserRef.addEventListener('loaderror', function(params){
error(1, params)
inAppBrowserRef.close()
inAppBrowserRef = undefined
});
}
}, false);
}
- 調用
jump = (url) => {
window.goThirdSite && window.goThirdSite(url, () => {
}, () => {
}, (code, params) => {
if (code === 0) {
this.props.warning('暫未開放此功能')
} else {
this.props.warning('加載失敗')
console.log(params)
}
})
}