為了方便給出前一篇地址:http://blog.csdn.net/weiyirong/article/details/7380651 調了幾天這個root許可權擷取問題終於搞定了,各種百度Google,各種方法全部都測試過終於有眉目了 .
我通過這幾天測試總結了三個方法擷取root許可權問題:
1 上一篇文章所引用的方法
[java]
view plaincopy
- /**
- * 應用程式運行命令擷取 Root許可權,裝置必須已破解(獲得ROOT許可權)
- *
- * @param command
- * 命令:String apkRoot="chmod 777 "+getPackageCodePath();//<span style="color:#cc0000;"><strong>注意這裡的getPackageCodePath()是繼承Activity裡的方法
- </strong></span> * RootCommand(apkRoot);
- * @return 應用程式是/否擷取Root許可權
- */
- public static boolean RootCommand(String command) {
- Process process = null;
- DataOutputStream os = null;
- try {
- process = Runtime.getRuntime().exec("su");
- os = new DataOutputStream(process.getOutputStream());
- os.writeBytes(command + "\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]
view plaincopy
- chmod 777 getPackageCodePath()
有的地方是這樣寫的
[java]
view plaincopy
- chmod 777 /dev/block/mmcblk0
經過幾次的測試,我發現後面那一句的話雖然能夠成功擷取許可權,但是!但是運行後的狀況是:手機運行,但是程式卡死,時不時的跳出是否強制關閉,我剛開始以為是耗時工作必須放線上程裡才不會卡死,就用了非同步線程和Thread測試,發現程式仍然卡死,當然有的時候在模擬器上可以運行,而在真機上卻卡死也有可能是耗時操作導致,需開線程處理。 但是在這裡的情況線程明顯不是問題,調了好久,我索性就把許可權擷取注釋掉,發現可以用!運行正常,真機模擬器都可以,這下問題找到了吧!我嘗試著把命令改成第一種測試,OK了!!!所有大夥注意這兩個的區別。(註:個人認為第二種雖然擷取得許可權,但是擷取許可權的進程很快就關閉了,所以接下去的操作仍有問題,而第一種是擷取為此應用程式的擷取許可權,因此可以正常執行,當然這隻是個人見解,有不同看法大家可以指點指點我哈)。
2. RootExplorer擷取root許可權的方法(以下是來自RootExplorer的源碼)
[java]
view plaincopy
- 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);
經過我的測試也是可行的,不過問題還是一樣的,就是黑屏,還會時而跳出是否強制關閉程式。傷不起
3 來自Google http://code.google.com/p/superuser/(關於Superuser超級管理器大夥自個百度之),下面是他的擷取root許可權源碼
[java]
view plaincopy
- 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();
- }
這種方法我測試了下,沒辦法,估計還要改一些地方,不推薦使用,當然可以研究研究呵呵,
轉自:http://blog.csdn.net/weiyirong/article/details/7400036