方法
在代码中执行adb shell指令主要是通过Runtime.getRuntime().exec
方法来执行指令,结果可以从Process的输入流中获取到。
示例代码
示例方法如下:
fun actionAdbShell(cmd: String) {
var s = StringBuilder()
try {
var process: Process = Runtime.getRuntime().exec(" $cmd")
var inputStream = process.inputStream
var bufferedReader = BufferedReader(InputStreamReader(inputStream))
try {
process.waitFor()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
var line: String? = bufferedReader.readLine()
while (line != null) {
s.append(line).append("\n");
line = bufferedReader.readLine()
}
inputStream.close()
bufferedReader.close()
} catch (e: Exception) {
e.printStackTrace()
LogUtil.d("异常信息:" + e.message)
}
LogUtil.d("执行结果:$s")
}
测试,例如要查看设备属性信息,调用如下:
AdbUtil.actionAdbShell("getprop")
输出部分结果如下:
[DEVICE_PROVISIONED]: [1]
[aaudio.hw_burst_min_usec]: [2000]
[aaudio.mmap_exclusive_policy]: [2]
[aaudio.mmap_policy]: [2]
[af.fast_track_multiplier]: [1]
[audio.deep_buffer.media]: [true]
[audio.offload.buffer.size.kb]: [32]
[audio.offload.gapless.enabled]: [true]
[audio.offload.min.duration.secs]: [30]
[audio.offload.video]: [true]
[audio.sys.mute.latency.factor]: [2]
[audio.sys.noisy.broadcast.delay]: [500]
[audio.sys.offload.pstimeout.secs]: [3]
[audio.sys.routing.latency]: [0]
[audio_para_version]: [J19S-Audiopara-cn-V01-20200918]
可以看到能够查看到对应指令的执行结果,和adb shell getprop
一样。
网友评论