无需所解释,直接上代码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(); } } }