scan-code.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <view class="content"></view>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. activity: null,
  9. receiver: null,
  10. intentFilter: null
  11. }
  12. },
  13. created: function(option) {
  14. this.initScan()
  15. this.startScan();
  16. },
  17. onHide: function() {
  18. this.stopScan();
  19. },
  20. destroyed: function() {
  21. //页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果/
  22. this.stopScan();
  23. },
  24. methods: {
  25. initScan() {
  26. let _this = this;
  27. this.activity = plus.android.runtimeMainActivity(); //获取activity
  28. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  29. this.intentFilter = new IntentFilter();
  30. this.intentFilter.addAction('nlscan.action.SCANNER_RESULT') // 换你的广播动作
  31. this.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
  32. onReceive: function(context, intent) {
  33. console.log("intent", intent)
  34. plus.android.importClass(intent);
  35. let content = intent.getStringExtra('SCAN_BARCODE1'); // 换你的广播标签
  36. uni.$emit('scancodedate', content)
  37. }
  38. });
  39. },
  40. startScan() {
  41. this.activity.registerReceiver(this.receiver, this.intentFilter);
  42. },
  43. stopScan() {
  44. this.activity.unregisterReceiver(this.receiver);
  45. }
  46. }
  47. }
  48. </script>
  49. <style>
  50. </style>