重载的dispatchKeyEvent方法在内部添加一些后处理代码时会冻结应用程序

问题描述 投票:0回答:1

我目前正在使用条形码扫描仪扫描某些条形码的应用程序。因此,我通过重载dispatchKeyEvent方法来捕获数据。只要我不尝试对提示的条形码进行后处理,一切都可以正常工作。

例如,此代码段按预期工作。

private var promptedEAN:String = ""
override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        if(event?.action == KeyEvent.ACTION_DOWN) {

            val pressedKey: Char = event.getUnicodeChar().toChar()
            promptedEAN += pressedKey

            val eTMengeKomm = findViewById<EditText>(R.id.eTMengeKomm)
            eTMengeKomm.setText(promptedEAN, TextView.BufferType.EDITABLE)

            val etCharge = findViewById<EditText>(R.id.eTCharge)
            etCharge.setText(promptedEAN, TextView.BufferType.EDITABLE)

            val etMHD = findViewById<EditText>(R.id.eTMHD)
            etMHD.setText(promptedEAN, TextView.BufferType.EDITABLE)
       }

       return super.dispatchKeyEvent(event)
}

BUT,一旦我想对dispatchKeyEvent方法中的提示条形码进行后处理,整个应用程序freezes。在这种情况下,我必须转到“设置”->“应用程序”->“强制停止”以终止我的应用程序,因为这两个按钮都不起作用。

我的后处理代码可以正常工作-我在活动的onCreate方法中使用测试字符串对其进行了尝试。

我的问题是为什么我正在通过dispatchKeyEvent方法得到这种行为?

这是我的代码。我在OnCreate方法中用键-值对填充HashMap,该方法是在触发dispatchKeyEvent之前启动的。

    private var promptedEAN:String = ""
    private var testPromptedEAN:String = "" //"C10207613037926927200015200600370098$1092547017L4"
    private val hashMap : HashMap<String, Int> = HashMap()

    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
        if(event?.action == KeyEvent.ACTION_DOWN) {

            val pressedKey: Char = event.getUnicodeChar().toChar()
            promptedEAN += pressedKey
            testPromptedEAN = promptedEAN


            val eTMengeKomm = findViewById<EditText>(R.id.eTMengeKomm)
            eTMengeKomm.setText(promptedEAN, TextView.BufferType.EDITABLE)

            val etCharge = findViewById<EditText>(R.id.eTCharge)
            etCharge.setText(promptedEAN, TextView.BufferType.EDITABLE)

            val etMHD = findViewById<EditText>(R.id.eTMHD)
            etMHD.setText(promptedEAN, TextView.BufferType.EDITABLE)

            if (testPromptedEAN.contains("C1",true) or testPromptedEAN.contains("C0",true)) { // EAN-128 Code
                var pos:Int = 2 // init pos
                var tail:String = testPromptedEAN.substring(pos) // init tail
                var lookInString:String =""

                    while (pos<testPromptedEAN.length-1) {
                        var searchString:String = testPromptedEAN.substring(pos, pos+2) // look into 2-digit codes first
                        if (hashMap.containsKey(searchString)){
                        var lengthData4code:Int = hashMap.getValue(searchString) // get length of data for code from hash table
                        if (tail.length<lengthData4code){
                            lengthData4code = tail.length
                            lookInString = tail.substring(searchString.length, lengthData4code)

                        } else {
                            lookInString = tail.substring(searchString.length, searchString.length+lengthData4code) // data (part)string
                        }

                        if (lookInString.indexOf("$")>=0) { // string size is actually shorter than expected
                        lookInString = tail.substring(searchString.length,searchString.length+lookInString.indexOf("$"))
                        pos += (searchString.length + lookInString.length + 1)
                        } else {
                        pos += (searchString.length+ lengthData4code)
                        }

                        if (pos>=testPromptedEAN.length-1) {tail = ""}
                        else {tail = testPromptedEAN.substring(pos)}


                        if (searchString.contains("37", false)) {
                        //val eTMengeKomm = findViewById<EditText>(R.id.eTMengeKomm)
                        eTMengeKomm.setText(lookInString, TextView.BufferType.EDITABLE)
                        }

                        if (searchString.contains("10", false)) {
                        //val etCharge = findViewById<EditText>(R.id.eTCharge)
                        etCharge.setText(lookInString, TextView.BufferType.EDITABLE)
                        }

                        if (searchString.contains("15", false)) {
                        //val etMHD = findViewById<EditText>(R.id.eTMHD)
                        etMHD.setText(lookInString, TextView.BufferType.EDITABLE)
                        }
                    }
                }
            }

        }
        return super.dispatchKeyEvent(event)
    }

非常感谢您的帮助。

android kotlin barcode-scanner keyevent
1个回答
0
投票

我如何解决这个问题:

首先,我摆脱了重载的dispachKeyEvent方法。这在我的应用程序中造成了很多混乱,就我而言,后退和中间按钮根本没有反应。条形码USB扫描仪应该在大多数情况下都可以从Android识别为外部键盘,因此不需要使用此方法将条形码捕获到特定的EditText字段中。之后,我将焦点放在第一个EditText字段上,并在此处提示条形码。

第二,我了解到活动的UI线程中存在while循环不起作用-这就是为什么我的应用程序一直冻结的原因。这就是为什么我在Activity中将AsyncTask实现为内部类,并将所有背景处理逻辑放入AsyncTask中重载的doInBackround方法中的原因。在onPostExecute方法中,我为EditText字段设置了所有必需的值。

感谢SO给我提供学习新事物的机会:]。>

© www.soinside.com 2019 - 2024. All rights reserved.