android - 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,但它不工作。
当应用程序关闭且屏幕锁定时,以相同性能执行任务的最佳方式是什么。
解决方案
推荐阅读
- c# - 如何将复杂的 C# 类序列化为 XML 并将其作为 .net 核心 API 的响应发送?
- python - 如何将 python 项目连接到我的 HTML CSS 网站
- r - 如果数据大小相同,为什么 TTR 包中的 EMA 会引发错误?(没有足够的非 NA 值错误)
- python - 图像像素中的 Dfs
- swift - 无法转换类型“DataResponse”的值
' 到预期的参数类型 'DataResponse ' - javascript - 日期和时间函数格式
- python - 如何模拟价值 sqlalchemy 模型的混合属性?
- bash - 如何编写一个 bash 脚本来递归计算每个文档中的单词数?
- flutter - 如何在使用颤振从侧面菜单抽屉导航时取消隐藏应用栏和底部导航栏?
- javascript - 用户关闭弹窗后如何申请优惠码?