Android 扫码枪不使用输入框获取扫描值

dispatchKeyEvent获取扫描结果

这是在CSDN上看到的评论,打印出来的并没有有关扫描的信息,我把打印的日志放在了最外层,发现其中一条是有关扫描的条码 event 

Action:ACTION_MULTIPLE,keyCode:KEYCODE_UNKNOWN

dispatchKeyEvent: KeyEvent { action=ACTION_MULTIPLE, keyCode=KEYCODE_UNKNOWN, scanCode=0, characters="3433422404403", metaState=0, flags=0x0, repeatCount=0, eventTime=365996, downTime=365996, deviceId=-1, source=0x101 }

 

获取扫描文本

 上面获取扫描文字使用 event.getUnicodeChar() 获取,目前我打印出来的结果是 :0,上面打印的结果,我猜想是 event.getCharacters()

 

onKeyMultiple

action的类型是 ACTION_MULTIPLE,可直接通过重写onKeyMultiple方法去过滤掉其他的event

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        Log.e("TAG", "onKeyMultiple------: " + event.toString());
        Log.i("TAG", "onKeyMultiple------: " + event.getCharacters());
        return super.onKeyMultiple(keyCode, repeatCount, event);
    }

打印结果

E/TAG: onKeyMultiple------: KeyEvent { action=ACTION_MULTIPLE, keyCode=KEYCODE_UNKNOWN, scanCode=0, characters="3433422404403", metaState=0, flags=0x0, repeatCount=0, eventTime=3548959, downTime=3548959, deviceId=-1, source=0x101 }
I/TAG: onKeyMultiple------: 3433422404403

 

event.getCharacters()弃用问题 (返回String)

https://stackoverflow.com/questions/59103860/android-keyevent-getcharacters-or-keyevent-characters-is-deprecated

并未找到可替代的方案。目前使用这种方式解决了现有问题

posted @ 2021-09-13 14:07  茄子鱼  阅读(2221)  评论(0编辑  收藏  举报