首页 > 解决方案 > HCE:设备锁定且屏幕关闭时在后台执行方法

问题描述

我正在尝试运行一段代码。当 Android 设备放在读卡器上并且读卡器发送一些命令时,将执行此代码。问题:程序响应缓慢,设备屏幕关闭时方法执行时间增加 3 倍。

我尝试按照 Android 文档PowerManager中的描述添加WAKE_LOCK

当 Android HostApduService接收到 apdu 时获取带有标志的WAKE_LOCK 并在设备离开读卡器时释放相同的标志。

代码:

override fun processCommandApdu(commandApdu: ByteArray?, extras: Bundle?): ByteArray {
 wakeUpScreen()
//rest of the task
retun ByteArray;
}

override fun onDeactivated(reason: Int) {
releaseWakeUp()
}

private fun wakeUpScreen() {
        try {
            if (fullWakeLock == null) {
                val powerManager =
                    this.getSystemService(Context.POWER_SERVICE) as PowerManager
                fullWakeLock = powerManager.newWakeLock(
                    PowerManager.SCREEN_DIM_WAKE_LOCK
                            or PowerManager.ACQUIRE_CAUSES_WAKEUP,
                    javaClass.canonicalName
                )
            }
  if ((fullWakeLock != null) &&
                (fullWakeLock!!.isHeld() == false)
            ) {                fullWakeLock!!.acquire()
           }
        } catch (e: Exception) {
            Log.e("TAG", e.printStackTrace().toString())
        }
    }

private fun releaseWakeUp() {
    if (fullWakeLock != null) {
        if (fullWakeLock!!.isHeld)
            fullWakeLock!!.release()
        fullWakeLock = null;
    }
}

查询:如 android 文档中所述,PARTIAL_WAKE_LOCK 确保运行 CPU,但它不工作。

当应用程序关闭且屏幕锁定时,以相同性能执行任务的最佳方式是什么。

标签: androidemulationhcecardmobileemulation

解决方案


推荐阅读