首先必须先引用hb的mui.min.js。
mui.plusReady(function() {
var receiver;
var main;
var gb = "这里录入广播参数的动作.";
var code = "这里录入广播参数的条码字符串";
main = plus.android.runtimeMainActivity(); //获取activity
var Intent = plus.android.importClass('android.content.Intent');
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, Intent) { //实现onReceiver回调函数,
var tm = Intent.getStringExtra(code);//获取条码信息.
tm = tm.replace(/[\r\n]/g,"");//这里去掉回车
}
}
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var filter = new IntentFilter();
filter.addAction(gb); //监听条码广播
switch(plus.device.vendor){
case "Mobydata": //根据不同的设备来设置是否要增加广播类别.
//filter.addCategory("com.android.decodewedge.decode_category");
filter.addCategory("com.type");
break;
}
main.registerReceiver(receiver, filter); //注册监听
}