應用程序有時候需要獲取root權限的時候,需要向手機發送root請求,不過前提是手機必須root纔有意義,那麼就存在檢測手機是否root,之後纔可以請求root權限
下面是檢測是否獲取了root權限
/**
* 應用程序運行命令獲取 Root權限,設備必須已破解(獲得ROOT權限)
*
* @return 應用程序是/否獲取Root權限
*/
public static boolean upgradeRootPermission(String pkgCodePath) {
Process process = null;
DataOutputStream os = null;
try {
String cmd="chmod 777 " + pkgCodePath;
process = Runtime.getRuntime().exec("su"); //切換到root帳號
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
請求root權限的方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//當前應用的代碼執行目錄
upgradeRootPermission(getPackageCodePath());
}