安卓扫码枪的问题

  • 欢迎关注我的公众号
    安卓扫码枪的问题_第1张图片
    公众号
  • 扫码枪等于输入设备
  • Activity监听

  override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        println("shiming MainActivity 扫码枪 dispatchKeyEvent= ")
        val mainActivity = this
        when (event?.action) {
            KeyEvent.ACTION_DOWN -> {
                val unicodeChar = event.unicodeChar
                mQRMsg.append(unicodeChar.toChar())
                if (event.keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                    return super.dispatchKeyEvent(event)
                }
                if (event.keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                    return super.dispatchKeyEvent(event)
                }
                if (event.keyCode == KeyEvent.KEYCODE_BACK) {
                    return super.dispatchKeyEvent(event)
                }
                if (event.keyCode == KeyEvent.KEYCODE_MENU) {
                    return super.dispatchKeyEvent(event)
                }
                if (event.keyCode == KeyEvent.KEYCODE_HOME) {
                    return super.dispatchKeyEvent(event)
                }
                if (event.keyCode == KeyEvent.KEYCODE_POWER) {
                    return super.dispatchKeyEvent(event)
                }
                val len = mQRMsg.length
                Observable.just("ARMSG").delay(200, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : io.reactivex.Observer {
                        override fun onSubscribe(d: Disposable) {

                        }
                        override fun onNext(value: String) {

                        }
                        override fun onError(e: Throwable) {

                        }
                        override fun onComplete() {
                            if (len != mQRMsg.length) return

                            if (mQRMsg.isNotEmpty()) {
                                //去除杂质符号
                                val code = mQRMsg.toString().replace("[^0-9a-zA-Z]".toRegex(), "")
                                EventBusActivityScope.getDefault(mainActivity)
                                    .post(BarCodeScanningGun(code))
                                ToastNativeLayoutUtils.toast("扫描枪的东西 MainA"+mQRMsg)
                                mQRMsg.setLength(0)
                            }
                        }
                    })
                return true
            }
        }
        return super.dispatchKeyEvent(event)
    }
  • 但是在Activity有Dialog弹出的话,就会监听不到
  • 1、这个 DialogdispatchKeyEvent方法行不通 ,也有可能我的姿势不对
    /* 监听*/
//    private StringBuffer mQRMsg = new StringBuffer();
//    @Override
//    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
//
////        switch (event.getKeyCode()) {
////            case KeyEvent.ACTION_DOWN:
////                int unicodeChar = event.getUnicodeChar();
////                mQRMsg.append((char) unicodeChar);
////                String code = mQRMsg.toString().replace("[^0-9a-zA-Z]", "");
////                ToastNativeLayoutUtils.INSTANCE.toast("扫描枪的东西 dispatchKeyEvent "+code);
////                mQRMsg.setLength(0);
////                return super.dispatchKeyEvent(event);
////        }
//        System.out.println("");
//        int unicodeChar = event.getUnicodeChar();
//        mQRMsg.append((char) unicodeChar);
//        String code = mQRMsg.toString().replace("[^0-9a-zA-Z]", "");
//        ToastNativeLayoutUtils.INSTANCE.toast("扫描枪的东西 dispatchKeyEvent "+code);
//        System.out.println("shiming code=="+code);
//        mQRMsg.setLength(0);
//        return super.dispatchKeyEvent(event);
//    }
  • 2、这个 Dialog 中 正确的回调
  mWaitingForCustomerDialog?.setOnKeyListener(object :DialogInterface.OnKeyListener{
            override fun onKey(p0: DialogInterface?, p1: Int, event: KeyEvent?): Boolean {
                when ( event?.action) {
                    KeyEvent.ACTION_DOWN -> {
                        val unicodeChar = event.unicodeChar
                        mQRMsg.append(unicodeChar.toChar())
                        if (event.keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                            return false
                        }
                        if (event.keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                            return false
                        }
                        if (event.keyCode == KeyEvent.KEYCODE_BACK) {
                            return false
                        }
                        if (event.keyCode == KeyEvent.KEYCODE_MENU) {
                            return false
                        }
                        if (event.keyCode == KeyEvent.KEYCODE_HOME) {
                            return false
                        }
                        if (event.keyCode == KeyEvent.KEYCODE_POWER) {
                            return false
                        }
                        val len = mQRMsg.length
                        Observable.just("ARMSG").delay(200, TimeUnit.MILLISECONDS)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(object : io.reactivex.Observer {
                                override fun onSubscribe(d: Disposable) {

                                }
                                override fun onNext(value: String) {

                                }
                                override fun onError(e: Throwable) {

                                }
                                override fun onComplete() {
                                    if (len != mQRMsg.length) return
                                    if (mQRMsg.isNotEmpty()) {
                                        //去除杂质符号
                                        val code = mQRMsg.toString().replace("[^0-9a-zA-Z]".toRegex(), "")
                                        ToastNativeLayoutUtils.toast("扫描枪的东西code: "+code)
                                        if (mWaitingForCustomerDialog?.isShowing!!){
                                            mWaitingForCustomerDialog?.dismiss()
                                        }
                                        mQRMsg.setLength(0)
                                    }
                                }
                            })
                    }
                }
                return true
            }
        })

你可能感兴趣的:(安卓扫码枪的问题)