2012-11-23 117 views
2

我已經建立了我的應用程序使用此代碼來執行su命令:檢查是否授予root權限?

try { 
      Runtime.getRuntime().exec("su"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      altDialog.setTitle("No Root"); 
      altDialog 
        .setMessage("I am afraid I have been unable to execute the su binary. Please check your root status."); 
      altDialog.setCancelable(false); 
      altDialog.setButton("Exit App", 
        new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface arg0, int arg1) { 
          // TODO Auto-generated method stub 
          Log.e("Android .img Flasher", 
            "Exiting due to root error"); 
          finish(); 
         } 
        }); 
     } 

如果su命令不存在(我相信)這捕獲,但如果根實際上是理所當然的。

我如何能夠檢查root是否被實際授予?

在附註中,我將如何使用Runtime.getRuntime.exec()命令來存儲命令的輸出?

回答

2

您可以使用下面的代碼。我已經寫了它用於通用命令的使用,但它也適用於su命令。如果命令成功以及命令輸出(或錯誤),它將返回。

public static boolean execCmd(String command, ArrayList<String> results){ 
    Process process; 
    try { 
     process = Runtime.getRuntime().exec(new String [] {"sh", "-c", command}); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    int result; 
    try { 
     result = process.waitFor(); 
    } catch (InterruptedException e1) { 
     e1.printStackTrace(); 
     return false; 
    } 

    if(result != 0){ //error executing command 
     Log.d("execCmd", "result code : " + result); 
     String line; 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
     try { 
      while ((line = bufferedReader.readLine()) != null){ 
       if(results != null) results.add(line); 
       Log.d("execCmd", "Error: " + line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return false; 
     } 
     return false; 
    } 

    //Command execution is OK 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

    String line; 
    try { 
     while ((line = bufferedReader.readLine()) != null){ 
      if(results != null) results.add(line); 
      Log.d("execCmd", line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 

你用兩個參數調用它:

  • command - 字符串命令來執行
  • results - 空的ArrayList返回命令的輸出。如果爲null,則不返回輸出。

要檢查su命令,你可以做到以下幾點:

//Array list where the output will be returned 
ArrayList<String> results = new ArrayList<String>(); 
//Command to be executed 
String command = "su -c ls"; 
boolean result = execCmd(command,results); 
//result returns command success 
//results returns command output 

問候。

0
public static boolean isRootAvailable(){ 
      Process p = null; 
      try{ 
       p = Runtime.getRuntime().exec(new String[] {"su"}); 
       writeCommandToConsole(p,"exit 0"); 
       int result = p.waitFor(); 
       if(result != 0) 
        throw new Exception("Root check result with exit command " + result); 
       return true; 
      } catch (IOException e) { 
       Log.e(LOG_TAG, "Su executable is not available ", e); 
      } catch (Exception e) { 
       Log.e(LOG_TAG, "Root is unavailable ", e); 
      }finally { 
       if(p != null) 
        p.destroy(); 
      } 
      return false; 
     } 
private static String writeCommandToConsole(Process proc, String command, boolean ignoreError) throws Exception{ 
      byte[] tmpArray = new byte[1024]; 
      proc.getOutputStream().write((command + "\n").getBytes()); 
      proc.getOutputStream().flush(); 
      int bytesRead = 0; 
      if(proc.getErrorStream().available() > 0){ 
       if((bytesRead = proc.getErrorStream().read(tmpArray)) > 1){ 
        Log.e(LOG_TAG,new String(tmpArray,0,bytesRead)); 
        if(!ignoreError) 
         throw new Exception(new String(tmpArray,0,bytesRead)); 
       } 
      } 
      if(proc.getInputStream().available() > 0){ 
       bytesRead = proc.getInputStream().read(tmpArray); 
       Log.i(LOG_TAG, new String(tmpArray,0,bytesRead)); 
      } 
      return new String(tmpArray); 
     }