我试图用java获取android shell命令'getprop'的输出,因为无论如何,getprop()总是返回null。
我是从developer.android.com尝试过的:
Process process = null;
try {
process = new ProcessBuilder()
.command("/system/bin/getprop", "build.version")
.redirectErrorStream(true)
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = process.getInputStream();
//String prop = in.toString();
System.out.println(in);
process.destroy();
然而,打印的不是输出而是一堆字符和数字(现在没有确切的输出)。
如何获得流程的输出?
谢谢!
答案 0 :(得分:31)
是否有任何特殊原因要将命令作为外部进程运行? 有一种更简单的方法:
String android_rel_version = android.os.Build.VERSION.RELEASE;
但是,如果你真的想通过shell命令来实现它,那么这就是我开始工作的方式:
try {
// Run the command
Process process = Runtime.getRuntime().exec("getprop");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Grab the results
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line + "\n");
}
// Update the view
TextView tv = (TextView)findViewById(R.id.my_text_view);
tv.setText(log.toString());
} catch (IOException e) {
}