無需所解釋,直接上代碼java
public synchronized boolean getRootAhth() { Process process = null; DataOutputStream os = null; try { //在軟件一樣的環境下執行獲取super user的權限 process = Runtime.getRuntime().exec("su"); //而後向控制檯寫入一條命令 os = new DataOutputStream(process.getOutputStream()); os.writeBytes("exit\n"); os.flush(); //讓調取控制檯的線程阻塞,等待控制檯的命令執行完成,最後返回控制檯推出的返回碼 int exitValue = process.waitFor(); if (exitValue == 0) { //控制檯正常退出,證實控制檯已經用super user權限執行完命令 return true; } else { //不然可能返回其餘的值 return false; } } catch (Exception e) { Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: " + e.getMessage()); return false; } finally { try { //最後關閉流和控制檯進程 if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); } } }