2012-06-29 29 views
9

我想要得到android shell命令'getprop'与java的输出,因为getprop()总是返回null,无论如何。如何读取Android进程命令的输出

我想这从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

你试过'.getInputStream ().toString();'而不是'.getInputStream();'...只是一个想法 – Zillinium

回答

21

是否有任何特定的原因,为什么你想运行该命令作为外部过程? 有一个简单的方法:

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) { 
}