歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Android應用請求獲取Root權限

Android應用請求獲取Root權限

日期:2017/2/28 14:49:16   编辑:Linux教程

要讓Android應用獲得Root權限,首先Android設備必須已經獲得Root權限。

應用獲取Root權限的原理:讓應用的代碼執行目錄獲取最高權限。在Linux中通過chmod 777

[代碼]java代碼:


/**

* 應用程序運行命令獲取 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;

}

調用代碼:

[代碼]java代碼:

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//當前應用的代碼執行目錄

upgradeRootPermission(getPackageCodePath());

}

執行上述代碼後,系統會彈出對話框“是否允許獲取Root權限”,此時選擇允許即可。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved