Android 扫码盒子全局接收付款码(全局事件&上层接收&多重下发)

 实现MainActivity,然后接收到的内容,通过接口回调,依次下发到各个业务

 还有另一种用EditText接收付款码的方式,地址:https://blog.csdn.net/qq_39731011/article/details/106383787

    String barcode = "";  // 收到的码结果
    String check = "0000";   //特殊码前缀 如:核销码
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getAction()==KeyEvent.ACTION_DOWN){
            Log.e("dispatchKeyEvent","dispatchKeyEvent: "+event.toString());
            char pressedKey = (char) event.getUnicodeChar();
            barcode += pressedKey;
            Log.e("dispatchKeyEvent","barcode: "+barcode);
        }
        if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            Log.e("dispatchKeyEvent","barcode最后: "+barcode);
            //  Toast.makeText(MainActivity.this,barcode,Toast.LENGTH_LONG).show();
            if(scanKeyManager!=null){
                String str = barcode.trim().replace("\n","");
                Log.e("付款码:", str + "");//check78787
                if (str.length() == 18) {//1346 0952 2362 9255 02
                    if (scanKeyManager != null && !str.startsWith(check)){
                        scanKeyManager.analysisKeyEvent(str);
                    }
                }
            }else{
                if(barcode.startsWith(check)){
                    getOrderEnsure(barcode.substring(4).replace("\n",""));
                }
            }
            barcode="";
        }
        return super.dispatchKeyEvent(event);
    }

 

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页