程序内的命令行 - 并非所有命令都会执行问题
我编写了一些运行 android 命令行并收集输出的代码。
它正确执行“ls”,但是当我输入命令“top -n 1”时,它什么也没显示。
这是一个明显的问题吗?手机未root,当使用“终端模拟器”时,我可以看到“顶部”输出。
这是代码:
// ** execute command line and gather the output **//
final StringBuilder log = new StringBuilder();
try{
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("top");
commandLine.add("-n1");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(", \n");
}
log.append(", \n");
}
catch (IOException e){
}
谢谢, 一个。
I wrote some code that runs an android command line and collect the output.
it is executing "ls" correctly but when I put the command "top -n 1" it shows nothing.
is it a manifest issue? the phone is not rooted and when using "terminal emulator" I can see "top" output.
here is the code:
// ** execute command line and gather the output **//
final StringBuilder log = new StringBuilder();
try{
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("top");
commandLine.add("-n1");
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(", \n");
}
log.append(", \n");
}
catch (IOException e){
}
thanks,
A.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想展示一些代码。通常,使用
Runtime
运行的命令不会在 shell 中执行,因此您可能需要尝试使用“sh -c top -n 1”作为prog
参数。You might want to show some of your code. Generally, commands you run using
Runtime
are not executed in a shell, so you might want to try something like "sh -c top -n 1" as theprog
parameter.