Execute the adb shell command in the code

method

Executing the adb shell command in the code is mainly to Runtime.getRuntime().execexecute the command through the method, and the result can be obtained from the input stream of the Process.

sample code

An example method is as follows:

    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")
    }

For testing, for example, to view device attribute information, the call is as follows:

AdbUtil.actionAdbShell("getprop")

The output part results are as follows:

[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]

You can see that you can view the execution results of the corresponding instructions, which is the adb shell getpropsame as

Guess you like

Origin blog.csdn.net/cat_is_so_cute/article/details/125739583