如何在android studio中侦听来自shell命令的响应? [英] How do I listen for a response from shell command in android studio?
问题描述
在android终端模拟器中,我可以键入以下命令:
In an android terminal emulator I can type the following commands:
> su
> echo $(</sys/class/power_supply/battery/charge_rate)
,根据手机的充电方式,输出将为无",普通"或涡轮".我希望能够检索此输出并将其作为字符串值存储在我的程序中.
and depending on how the phone is charging the output will be "None", "Normal" or "Turbo". I would like to be able to retrieve this output and store it as a string value in my program.
因此,我对此进行了一些研究,得出的代码如下:
So I've done a little bit of research into this and the code I came up with is as follows:
String chargeRate = "None";
try {
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("su \"\"echo $(</sys/class/power_supply/battery/charge_rate)");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
if ((chargeRate = stdInput.readLine()) == null)
chargeRate = "None";
}
catch (Exception e) {
// TODO
}
这是从许多不同的答案中得出的,我不太确定这是怎么回事.在调试时,我无法越过或越过此行:
This is taken from a number of different answers and I'm not quite sure whats wrong with it. I can't step over or past this line while debugging:
if ((chargeRate = stdInput.readLine()) == null)
调试器到达此行后,立即显示应用程序正在运行"
As soon as the debugger reaches this line it says "The application is running"
推荐答案
UPDATE : Solution is in Unable using Runtime.exec() to execute shell command "echo" in Android Java code :
Runtime.getRuntime.exec()
不会直接执行shell命令,它执行带有参数的可执行文件."echo"是一个内置的外壳命令.它实际上是可执行文件sh的参数的一部分使用选项-c.像ls
这样的命令是实际的可执行文件.你可以在adb shell中使用type echo
和type ls
命令查看差异.
Runtime.getRuntime.exec()
doesn't execute a shell command directly, it executes an executable with arguments. "echo" is a builtin shell command. It is actually a part of the argument of the executable sh with the option -c. Commands likels
are actual executables. You can usetype echo
andtype ls
command in adb shell to see the difference.
最后的代码是:
String[] cmdline = { "sh", "-c", "echo $..." };
Runtime.getRuntime().exec(cmdline);
还可以从 Runtime.exec()
内部执行
cat
,而无需调用 sh
cat
is also executable from within Runtime.exec()
without invoking sh
https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html?page=2 命令是可执行程序
>执行shell命令并获取尽管TextView使用了直接可执行的命令( ls
,请参见上面的更新),但它在TextView中的输出还是不错的:
The code in Execute shell commands and get output in a TextView is good although it uses a command that is executable directly (ls
, see update above) :
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("ls -l");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
这篇关于如何在android studio中侦听来自shell命令的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!