Houjie
2 天以前 30590221cd34912a61f4734ea474859a0df98be6
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<template>  
    <view class="content"></view>  
</template>  
 
<script>  
var main,receiver,filter;    
var _codeQueryTag = false;    
export default {  
    name:"pdaScan",
    data() {  
        return {  
            scanCode: ''  
        }  
    },  
    created: function (option) {  
        // #ifdef APP-PLUS
        if (typeof plus !== 'undefined') {
            this.initScan()  
            this.startScan();    
        }
        // #endif
    },    
    onHide:function(){    
        // #ifdef APP-PLUS
        if (typeof plus !== 'undefined') {
            this.stopScan();    
        }
        // #endif
    },  
    destroyed:function(){    
        /*页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果*/    
        // #ifdef APP-PLUS
        if (typeof plus !== 'undefined') {
            this.stopScan();    
        }
        // #endif
    },    
    methods: {  
        initScan() {  
            let _this = this;  
            main = plus.android.runtimeMainActivity();//获取activity  
            var IntentFilter = plus.android.importClass('android.content.IntentFilter');   
            filter = new IntentFilter();    
            filter.addAction("android.intent.ACTION_DECODE_DATA"); // 换你的广播动作  
            receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver',{  
            onReceive : function(context, intent) {  
                plus.android.importClass(intent);     
                let code = intent.getStringExtra("barcode_string");// 换你的广播标签  
                _this.queryCode(code);    
            }});    
        },    
        startScan(){    
            main.registerReceiver(receiver,filter);  
        },    
        stopScan(){  
            main.unregisterReceiver(receiver);    
        },    
        queryCode: function(code){  
            //防重复  
            if(_codeQueryTag)return false;    
            _codeQueryTag = true;    
            setTimeout(function(){    
                _codeQueryTag = false;    
            },150);  
            var id = code  
            uni.$emit('scancodedate',{code:id})  
        }  
    }  
}  
</script>  
 
<style>  
    page {  
        background-color: #efeff4;  
    }  
    .content {  
        text-align: center;  
    }  
</style>