Android应用层如何执行shell脚本

应用层如何执行shell脚本,通过启动一个shell脚本服务执行

private fun execCmd(cmd: String, listener: (isExe: Boolean, result: String?) -> Unit) {
Thread(Runnable {
val cmdPath = "/storage/emulated/legacy/cmd"
val retPath = "/storage/emulated/legacy/ret"
FileIOUtils.writeFileFromString(cmdPath, cmd)
FileIOUtils.writeFileFromString(retPath, "")
SystemProperties.setProp("ctl.stop", "sdcard_it")
SystemProperties.setProp("persist.sys.ret.status", "0")
SystemProperties.setProp("persist.sys.cmd.file", cmdPath)
SystemProperties.setProp("persist.sys.ret.file", retPath)
SystemProperties.setProp("ctl.start", "sdcard_it")

var retryCount = 5
var isExe = false
do {
isExe = SystemProperties.getProp("persist.sys.ret.status") == "1"
Thread.sleep(200)
} while (!isExe && retryCount-- > 0)
SystemProperties.setProp("persist.sys.ret.status", "0")
val result = FileIOUtils.readFile2String(retPath)
listener(isExe, result)
}).start()
}

 

 

sdcard_it为一个服务,但是不启动,在应用层启动

service sdcard_it/system/bin/sh /system/etc/back.sh
    user root
    group root
    disabled
    oneshot

 

你可能感兴趣的:(android)