2011-02-08 69 views

回答

0

您可以使用此代碼段(從亞倫C)

void execCommandLine(String command) 
    { 
     Runtime runtime = Runtime.getRuntime(); 
     Process proc = null; 
     OutputStreamWriter osw = null; 

     try 
     { 
      proc = runtime.exec("su"); 
      osw = new OutputStreamWriter(proc.getOutputStream()); 
      osw.write(command); 
      osw.flush(); 
      osw.close(); 
     } 
     catch (IOException ex) 
     { 
      Log.e("execCommandLine()", "Command resulted in an IO Exception: " + command); 
      return; 
     } 
     finally 
     { 
      if (osw != null) 
      { 
       try 
       { 
        osw.close(); 
       } 
       catch (IOException e){} 
      } 
     } 

     try 
     { 
      proc.waitFor(); 
     } 
     catch (InterruptedException e){} 

     if (proc.exitValue() != 0) 
     { 
      Log.e("execCommandLine()", "Command returned error: " + command + "\n Exit code: " + proc.exitValue()); 
     } 
    } 

但這需要root訪問權限,我認爲。

你也可以嘗試使用GScript

+0

公共無效的onCreate(捆綁savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); try { \t Process pr; \t pr = Runtime.getRuntime()。exec(「top -n 1 -d 1」); \t System.out.println(「PRO EXEC」); \t BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); } catch(Exception e){ \t \t e.printStackTrace(); } ******************************************** – anshu 2011-02-09 04:35:45

0

我一直在使用這在我的Android應用程序運行shell腳本。唯一的事情我還沒有弄清楚如何去做就是將輸出導向到我想要的地方。你不需要root,這就是我發佈的原因。

Process process = Runtime.getRuntime().exec("top -n 1"); 
//Get the output of top so that it can be read 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));