Issue
I am able to execute shell commands (that has a return value) in Kotlin [Android] using the following lines of code:
fun getFrequencyLevelsCPU0(): Unit {
val process: java.lang.Process = java.lang.Runtime.getRuntime().exec("su -c cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies")
process.waitFor()
}
The above lines of code are capable of running the shell command but the output of the command should be something as follows if the command was written in the add shell:
500000 851000 984000 1106000 1277000 1426000 1582000 1745000 1826000 2048000 2188000 2252000 2401000 2507000 2630000 2704000 2802000
How can I get these above values returned in the getFrequencyLevelsCPU0() function in Kotlin after executing the shell command?
Solution
Since you have a java.lang.Process
, you can use its getInputStream()
(in Kotlin it can be shortened to just inputStream
) (see JavaDoc here) to read the output, for example:
val output = process.inputStream.bufferedReader().lineSequence().joinToString("\n")
Answered By - Andrzej Zabost
Answer Checked By - Marie Seifert (JavaFixing Admin)