我试图用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();
Run Code Online (Sandbox Code Playgroud)
然而,打印的不是输出而是一堆字符和数字(现在没有确切的输出).
如何获得流程的输出?
谢谢!
小智 31
是否有任何特殊原因要将命令作为外部进程运行?有一种更简单的方法:
String android_rel_version = android.os.Build.VERSION.RELEASE;
Run Code Online (Sandbox Code Playgroud)
但是,如果你真的想通过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) {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11828 次 |
| 最近记录: |