【发布时间】:2015-04-30 18:55:31
【问题描述】:
我正在做一个简单的应用程序,显示 android 中的当前进程,就像一个 shell。
我的应用程序执行 ls、cd、makedir 和其他命令,但 top 或 htop 命令不执行。 (htop 无法识别,最重要的是,应用程序冻结)。我需要root吗?我已经在 unrooted android 中下载了终端应用程序,并且 top 命令可以正常工作。
我的应用有 2 个类。一个主体和一个外壳
主课
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShellExecuter exe = new ShellExecuter();
command = input.getText().toString();
String outp = exe.Executer(command);
out.setText(outp);
Log.d("Output", outp);
}
外壳类
public String Executer(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
String response = output.toString();
return response;
}
为什么应用程序的某些命令可以工作,而 top 例如却不能?
【问题讨论】: