通過廣播的方式進行掃描結果的接收。通過native.js使用原生安卓類庫
mui.plusReady(function() {
mui.init();
setTimeout(function() {
scan(function(code) {
console.log('掃描結果:' + code);
GetUpperShelfApp(code);
})
}, 10)
if(mui.os.android) {
var nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
plus.android.importClass(nativeWebview);
nativeWebview.requestFocus();
} else {
alert(2);
nativeWebview.plusCallMethod({
"setKeyboardDisplayRequiresUserAction": false
});
}
})
function pageInit() {
var _self = plus.webview.currentWebview()
localStorage.setItem('WEBVIEW_ID', _self.id)
mui.back = function() {
localStorage.setItem('WEBVIEW_ID', _self.opener().id)
_self.close();
}
}
function scan(callback) {
var main = plus.android.runtimeMainActivity(); //獲取activity
var receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) { //實現onReceiver回撥函數
callback(intent.getStringExtra('SCAN_BARCODE1'))//SCAN_BARCODE1為條碼1Extra
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //引入過濾器
var Intent = plus.android.importClass('android.content.Intent');
var filter = new IntentFilter();
filter.addAction('nlscan.action.SCANNER_RESULT'); //廣播輸出action//監聽掃碼廣播
main.registerReceiver(receiver, filter); //註冊監聽
mui.toast("初始化完成。");
}