為瞭方便給出上一篇上一篇地址: <a href="https://www.aiwalls.com/kf/201203/124585.html >
調瞭幾天這個root權限獲取問題終於搞定瞭,各種百度谷歌,各種方法全部都測試過終於有眉目瞭
我通過這幾天測試總結瞭三個方法獲取root權限問題:
1 上一篇文章所引用的方法
[html]
</pre><pre>
[html]
public class DemoActivity extends Activity {
public final String rootPowerCommand = "chmod 777 /dev/block/mmcblk0";// 授權root權限命令
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AlertDialog.Builder(this).setMessage(rootCommand(rootPowerCommand)+"….").show();
File []files = new File("/root").listFiles();
if(files==null){//<strong><span style="font-size:18px;color:#ff0000;">說明是NULL。。。。就是不能訪問其下的文件瞭
</span></strong> new AlertDialog.Builder(this).setMessage(".OK…").show();
}
// files[0].getName();
}
/**
* 授權root用戶權限
*
* @param command
* */
public boolean rootCommand(String command) {
Process process = null;
DataOutputStream dos = null;
try {
process = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(command + "\n");
dos.writeBytes("exit\n");
dos.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (dos != null) {
dos.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
}
[java]
<span style="font-family:Arial;BACKGROUND-COLOR: #ffffff"></span>其中我這裡是把dos.writeBytes("exit\n");去掉瞭,發現手機上提示獲取權限成功,但是問題來瞭,手機黑屏,程序還在運行,就是黑屏。。。。。等下還會跳出是否強制關閉,這個原因下面或解釋
[java]
<strong><span style="font-size:18px;"><span style="color:#3333ff;">2 <span class="link_title"><a title="RootExplorer怎麼樣獲取root權限的" href="http://blog.csdn.net/a345017062/article/details/6441986">RootExplorer獲取root權限的</a>方法(以下是來自RootExplorer的源碼)</span></span></span></strong>
[html]
<span style="font-size:18px;"><strong><span style="BACKGROUND-COLOR: #000000">ProcessBuilder pb = new ProcessBuilder("/system/bin/sh");
//java.lang.ProcessBuilder: Creates operating system processes.
pb.directory(new File("/"));//設置shell的當前目錄。
try {
Process proc = pb.start();
//獲取輸入流,可以通過它獲取SHELL的輸出。
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
//獲取輸出流,可以通過它向SHELL發送命令。
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc
.getOutputStream())), true);
out.println("pwd");
out.println("su root");//執行這一句時會彈出對話框(以下程序要求授予最高權限…),要求用戶確認。
out.println("cd /data/data");//這個目錄在系統中要求有root權限才可以訪問的。
out.println("ls -l");//這個命令如果能列出當前安裝的APK的數據文件存放目錄,就說明我們有瞭ROOT權限。
out.println("exit");
// proc.waitFor();
String line;
while ((line = in.readLine()) != null) {
System.out.println(line); // 打印輸出結果
}
while ((line = err.readLine()) != null) {
System.out.println(line); // 打印錯誤輸出結果
}
in.close();
out.close();
proc.destroy();
} catch (Exception e) {
System.out.println("exception:" + e);
</span>} </strong></span>
[java]
經過我的測試也是可行的,不過問題還是一樣的,就是黑屏,還會時而跳出是否強制關閉程序。
[java]
<span style="font-size:18px;"><strong>3 來自谷歌 </strong></span><a href="http://code.google.com/p/superuser/"><span style="font-size:18px;"><strong>http://code.google.com/p/superuser/</strong></span></a>,<em><strong>(關於</strong><span style="font-size:16px;color:#000000;">Superuser超級管理器大夥自個百度之),下面是他的獲取root權限源碼</span></em>
[java]
[html]
File superuser = new File("/system/bin/superuser");
if (superuser.exists())
{
// return device to original state
Process process = Runtime.getRuntime().exec("superuser");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -oremount,rw /dev/block/mtdblock3 /system\n");
os.writeBytes("busybox cp /system/bin/superuser /system/bin/su\n");
os.writeBytes("busybox chown 0:0 /system/bin/su\n");
os.writeBytes("chmod 4755 /system/bin/su\n");
os.writeBytes("rm /system/bin/superuser\n");
os.writeBytes("exit\n");
os.flush();
}
[java]
這種方法我測試瞭下,沒辦法,估計還要改一些地方,不推薦使用,當然可以研究研究呵呵
[java]
[java]
現在,來說明下為什麼會黑屏現象,從以上描述我們可以知道程序一定在運行!!!!隻是為什麼會顯示不瞭呢?關鍵點就在這,我們想想以前學習javaSE時寫的線程啊呵呵,不是在寫界面與線程方面的學習時,經常碰到一按按鈕(Button)界面就卡死嗎?我自己來說吧,在做一個類QQ的應用程序時,服務器弄瞭個按鈕,點擊就啟動服務器,可是一點擊他就卡死瞭,雖說服務器起來瞭,可是關不掉啊,服務器啟動和關閉界面卡死瞭(就跟Android的那個黑屏、顯示強制關閉一樣呵呵),當時顯然的就聯想到瞭那個線程方法來啟動服務器啊!! 所以現在這種思想也應該繼續保持哦 – -! 接下去咱不就不解釋瞭,大夥懂的!弄個線程。。解決之。。。。。
[java]
這個問題調試瞭n久,也弄瞭n種方法,模擬器又沒root(光拔插手機調試就不知道弄瞭幾次。。。 – -#),還有什麼不足之處,大夥多多補充啊!!
摘自 魏藝榮的專欄