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
48
49
50
51
52
53
| <template>
| <view class="content"></view>
| </template>
|
| <script>
| export default {
| data() {
| return {
| activity: null,
| receiver: null,
| intentFilter: null
| }
| },
| created: function(option) {
| this.initScan()
| this.startScan();
| },
| onHide: function() {
| this.stopScan();
| },
| destroyed: function() {
| //页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果/
| this.stopScan();
| },
| methods: {
| initScan() {
| let _this = this;
| this.activity = plus.android.runtimeMainActivity(); //获取activity
| var IntentFilter = plus.android.importClass('android.content.IntentFilter');
| this.intentFilter = new IntentFilter();
| this.intentFilter.addAction('nlscan.action.SCANNER_RESULT') // 换你的广播动作
| this.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
| onReceive: function(context, intent) {
| console.log("intent", intent)
| plus.android.importClass(intent);
| let content = intent.getStringExtra('SCAN_BARCODE1'); // 换你的广播标签
|
| uni.$emit('scancodedate', content)
| }
| });
| },
| startScan() {
| this.activity.registerReceiver(this.receiver, this.intentFilter);
| },
| stopScan() {
| this.activity.unregisterReceiver(this.receiver);
| }
| }
| }
| </script>
|
| <style>
| </style>
|
|