scan-code.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. /* #ifdef APP-PLUS */
  28. this.activity = plus.android.runtimeMainActivity(); //获取activity
  29. var context = plus.android.importClass('android.content.Context'); //上下文
  30. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  31. this.intentFilter = new IntentFilter();
  32. this.intentFilter.addAction('nlscan.action.SCANNER_RESULT') // 换你的广播动作
  33. this.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
  34. onReceive: function(context, intent) {
  35. console.log("intent",intent)
  36. plus.android.importClass(intent);
  37. let content = intent.getStringExtra('SCAN_BARCODE1'); // 换你的广播标签
  38. uni.$emit('scancodedate', content)
  39. }
  40. });
  41. /* #endif */
  42. },
  43. startScan() {
  44. /* #ifdef APP-PLUS */
  45. this.activity.registerReceiver(this.receiver, this.intentFilter);
  46. /* #endif */
  47. },
  48. stopScan() {
  49. /* #ifdef APP-PLUS */
  50. this.activity.unregisterReceiver(this.receiver);
  51. /* #endif */
  52. }
  53. }
  54. }
  55. </script>
  56. <style>
  57. </style>