12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <template>
- <div class="content"></div>
- </template>
-
- <script>
- var main,receiver,filter;
- var _codeQueryTag = false;
- export default {
- data() {
- return {
- scanCode: ''
- }
- },
- created () {
- // #ifdef APP-PLUS
- this.initScan();
- this.startScan();
- // #endif
- },
- onHide(){
- // #ifdef APP-PLUS
- this.stopScan();
- // #endif
- },
- destroyed(){
- /*页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果*/
- // #ifdef APP-PLUS
- 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("com.android.server.scannerservice.broadcast"); // 换你的广播动作
- receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver',{
- onReceive : function(context, intent) {
- plus.android.importClass(intent);
- let code = intent.getStringExtra("scannerdata");// 换你的广播标签
- _this.queryCode(code);
- }});
- },
- startScan(){
- main.registerReceiver(receiver,filter);
- },
- stopScan(){
- main.unregisterReceiver(receiver);
- },
- queryCode(code){
- //防重复
- if(_codeQueryTag)return false;
- _codeQueryTag = true;
- setTimeout(function(){
- _codeQueryTag = false;
- },150);
- var id = code
- uni.$emit('scan',id)
- }
- }
- }
- </script>
-
- <style>
- /* page {
- background-color: #efeff4;
- }
- .content {
- text-align: center;
- } */
- </style>
-
|