Android編程擷取root許可權問題

來源:互聯網
上載者:User

為了方便給出前一篇地址:http://blog.csdn.net/weiyirong/article/details/7380651  調了幾天這個root許可權擷取問題終於搞定了,各種百度Google,各種方法全部都測試過終於有眉目了  .

我通過這幾天測試總結了三個方法擷取root許可權問題:  

1 上一篇文章所引用的方法

[java]
view plaincopy
  1. /** 
  2.      * 應用程式運行命令擷取 Root許可權,裝置必須已破解(獲得ROOT許可權) 
  3.      *  
  4.      * @param command 
  5.      *            命令:String apkRoot="chmod 777 "+getPackageCodePath();//<span style="color:#cc0000;"><strong>注意這裡的getPackageCodePath()是繼承Activity裡的方法 
  6. </strong></span>     *            RootCommand(apkRoot); 
  7.      * @return 應用程式是/否擷取Root許可權 
  8.      */  
  9.     public static boolean RootCommand(String command) {  
  10.         Process process = null;  
  11.         DataOutputStream os = null;  
  12.         try {  
  13.             process = Runtime.getRuntime().exec("su");  
  14.             os = new DataOutputStream(process.getOutputStream());  
  15.             os.writeBytes(command + "\n");  
  16.             os.writeBytes("exit\n");  
  17.             os.flush();  
  18.             process.waitFor();  
  19.         } catch (Exception e) {  
  20.             return false;  
  21.         } finally {  
  22.             try {  
  23.                 if (os != null) {  
  24.                     os.close();  
  25.                 }  
  26.                 process.destroy();  
  27.             } catch (Exception e) {  
  28.             }  
  29.         }  
  30.         return true;  
  31.     }  

這個方法要注意的問題在那條命令

[java]
view plaincopy
  1. chmod 777 getPackageCodePath()  

有的地方是這樣寫的

[java]
view plaincopy
  1. chmod 777 /dev/block/mmcblk0  

經過幾次的測試,我發現後面那一句的話雖然能夠成功擷取許可權,但是!但是運行後的狀況是:手機運行,但是程式卡死,時不時的跳出是否強制關閉,我剛開始以為是耗時工作必須放線上程裡才不會卡死,就用了非同步線程和Thread測試,發現程式仍然卡死,當然有的時候在模擬器上可以運行,而在真機上卻卡死也有可能是耗時操作導致,需開線程處理。  但是在這裡的情況線程明顯不是問題,調了好久,我索性就把許可權擷取注釋掉,發現可以用!運行正常,真機模擬器都可以,這下問題找到了吧!我嘗試著把命令改成第一種測試,OK了!!!所有大夥注意這兩個的區別。(註:個人認為第二種雖然擷取得許可權,但是擷取許可權的進程很快就關閉了,所以接下去的操作仍有問題,而第一種是擷取為此應用程式的擷取許可權,因此可以正常執行,當然這隻是個人見解,有不同看法大家可以指點指點我哈)。

2. RootExplorer擷取root許可權的方法(以下是來自RootExplorer的源碼)

[java]
view plaincopy
  1. ProcessBuilder pb = new ProcessBuilder("/system/bin/sh");   
  2. //java.lang.ProcessBuilder:  Creates operating system processes.   
  3. pb.directory(new File("/"));//設定shell的目前的目錄。     
  4. try {    
  5.     Process proc = pb.start();    
  6.     //擷取輸入資料流,可以通過它擷取SHELL的輸出。     
  7.     BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));    
  8.     BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));    
  9.     //擷取輸出資料流,可以通過它向SHELL發送命令。     
  10.     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc    
  11.                     .getOutputStream())), true);    
  12.     out.println("pwd");    
  13.     out.println("su root");//執行這一句時會彈出對話方塊(以下程式要求授予最高許可權...),要求使用者確認。     
  14.     out.println("cd /data/data");//這個目錄在系統中要求有root許可權才可以訪問的。     
  15.     out.println("ls -l");//這個命令如果能列出當前安裝的APK的資料檔案存放目錄,就說明我們有了ROOT許可權。     
  16.     out.println("exit");    
  17.     // proc.waitFor();     
  18.     String line;    
  19.     while ((line = in.readLine()) != null) {    
  20.         System.out.println(line);   // 列印輸出結果  
  21.     }    
  22.     while ((line = err.readLine()) != null) {    
  23.         System.out.println(line);  // 列印錯誤輸出結果  
  24.     }    
  25.     in.close();    
  26.     out.close();    
  27.     proc.destroy();    
  28. } catch (Exception e) {    
  29.     System.out.println("exception:" + e);    

經過我的測試也是可行的,不過問題還是一樣的,就是黑屏,還會時而跳出是否強制關閉程式。傷不起
3 來自Google http://code.google.com/p/superuser/(關於Superuser超級管理器大夥自個百度之),下面是他的擷取root許可權源碼 

 

 

[java]
view plaincopy
  1. File superuser = new File("/system/bin/superuser");    
  2.    
  3. if (superuser.exists())  
  4. {  
  5.  // return device to original state  
  6.  Process process = Runtime.getRuntime().exec("superuser");  
  7.  DataOutputStream os = new DataOutputStream(process.getOutputStream());      
  8.  os.writeBytes("mount -oremount,rw /dev/block/mtdblock3 /system\n");  
  9.  os.writeBytes("busybox cp /system/bin/superuser /system/bin/su\n");  
  10.  os.writeBytes("busybox chown 0:0 /system/bin/su\n");  
  11.  os.writeBytes("chmod 4755 /system/bin/su\n");  
  12.  os.writeBytes("rm /system/bin/superuser\n");  
  13.  os.writeBytes("exit\n");  
  14.  os.flush();  
  15. }  

這種方法我測試了下,沒辦法,估計還要改一些地方,不推薦使用,當然可以研究研究呵呵,

轉自:http://blog.csdn.net/weiyirong/article/details/7400036

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.