有可能是在主线程执行了,要放到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
}