有可能是在主线程执行了,要放到IO执行。
报错代码:
private fun closeAfter3(){
lifecycleScope.launch{
delay(3000)
RootCommand("echo off > /sys/class/drm/card0-DSI-1/status")
}
}
正确代码:
private fun closeAfter3(){
lifecycleScope.launch(Dispatchers.IO) {
delay(3000)
RootCommand("echo off > /sys/class/drm/card0-DSI-1/status")
}
}
============================
另外附上android runtime exec 执行 echo 的方法:
要点注意:
1、由于是在设备本体上运行exec,不是在PC端去操作设备,所以用的“sh”,不是“adb shell”
2、如果对文件的操作失败,可能是文件权限未打开。需要使用chmod 777 /sys/class/drm/card0-DSI-1/status把权限打开。
方法本体:
//往gpio写
private fun RootCommand(command: String): Boolean {
var process: Process? = null
var os: DataOutputStream? = null
try {
val cmdline = arrayOf("sh", "-c", command)
Runtime.getRuntime().exec(cmdline)
} catch (e: Exception) {
return false
} finally {
try {
if (os != null) {
os.close()
}
process!!.destroy()
} catch (e: Exception) {
Logs.common.e("RootCommand error",e)
}
}
return true
}
文章讨论了一个在Android应用中执行设备操作的问题。原始代码在一个生命周期作用域内使用主线程执行延迟命令,导致潜在的阻塞问题。正确的做法是将该操作放在IO调度器下执行,以避免阻塞UI线程。此外,还提供了一个执行`echo`命令的方法,并提醒注意设备上的文件权限设置。
1305

被折叠的 条评论
为什么被折叠?



