判斷手機是否root

關於判斷手機是否已經root的方法。如果app有一些特殊功能需要root權限,則需要判斷是否root。比如一些市場下載完app後自動安裝。

[java] view plain copy
 print?
  1. /** 
  2.  * @author Kevin Kowalewski 
  3.  *  
  4.  */  
  5. public class Root {  
  6.   
  7.     private static String LOG_TAG = Root.class.getName();  
  8.   
  9.     public boolean isDeviceRooted() {  
  10.         if (checkRootMethod1()){return true;}  
  11.         if (checkRootMethod2()){return true;}  
  12.         if (checkRootMethod3()){return true;}  
  13.         return false;  
  14.     }  
  15.   
  16.     public boolean checkRootMethod1(){  
  17.         String buildTags = android.os.Build.TAGS;  
  18.   
  19.         if (buildTags != null && buildTags.contains("test-keys")) {  
  20.             return true;  
  21.         }  
  22.         return false;  
  23.     }  
  24.   
  25.     public boolean checkRootMethod2(){  
  26.         try {  
  27.             File file = new File("/system/app/Superuser.apk");  
  28.             if (file.exists()) {  
  29.                 return true;  
  30.             }  
  31.         } catch (Exception e) { }  
  32.   
  33.         return false;  
  34.     }  
  35.   
  36.     public boolean checkRootMethod3() {  
  37.         if (new ExecShell().executeCommand(SHELL_CMD.check_su_binary) != null){  
  38.             return true;  
  39.         }else{  
  40.             return false;  
  41.         }  
  42.     }  
  43. }  
  44.   
  45.   
  46. /** 
  47.  * @author Kevin Kowalewski 
  48.  * 
  49.  */  
  50. public class ExecShell {  
  51.   
  52.     private static String LOG_TAG = ExecShell.class.getName();  
  53.   
  54.     public static enum SHELL_CMD {  
  55.         check_su_binary(new String[] {"/system/xbin/which","su"}),  
  56.         ;  
  57.   
  58.         String[] command;  
  59.   
  60.         SHELL_CMD(String[] command){  
  61.             this.command = command;  
  62.         }  
  63.     }  
  64.   
  65.     public ArrayList<String> executeCommand(SHELL_CMD shellCmd){  
  66.         String line = null;  
  67.         ArrayList<String> fullResponse = new ArrayList<String>();  
  68.         Process localProcess = null;  
  69.   
  70.         try {  
  71.             localProcess = Runtime.getRuntime().exec(shellCmd.command);  
  72.         } catch (Exception e) {  
  73.             return null;  
  74.             //e.printStackTrace();  
  75.         }  
  76.   
  77.         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(localProcess.getOutputStream()));  
  78.         BufferedReader in = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));  
  79.   
  80.         try {  
  81.             while ((line = in.readLine()) != null) {  
  82.                 Log.d(LOG_TAG, "--> Line received: " + line);  
  83.                 fullResponse.add(line);  
  84.             }  
  85.         } catch (Exception e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.   
  89.         Log.d(LOG_TAG, "--> Full response was: " + fullResponse);  
  90.   
  91.         return fullResponse;  
  92.     }  
  93.   
  94. }  

代碼來自stackoverflow,向作者致敬。

方法2:

The RootTools library offers simple methods to check for root:

一個開源項目:http://code.google.com/p/roottools/

RootTools.isRootAvailable()判斷是否root

RootTools.isAccessGiven()返回true那麼手機已經root並且app也被授予root權限。

另外:據那片帖子的一個回貼人說使用

[java] view plain copy
 print?
  1. String commandToExecute = "su";  
  2. executeShellCommand(commandToExecute);  
  3. private boolean executeShellCommand(String command){  
  4.     Process process = null;              
  5.     try{  
  6.         process = Runtime.getRuntime().exec(command);  
  7.         return true;  
  8.     } catch (Exception e) {  
  9.         return false;  
  10.     } finally{  
  11.         if(process != null){  
  12.             try{  
  13.                 process.destroy();  
  14.             }catch (Exception e) {  
  15.             }  
  16.         }  
  17.     }  
  18. }  

會引起非常嚴重的性能問題,將手機系統拖的非常慢,當應用多次啓動後會創建出很多個僵死的進程耗用內存。
參考http://stackoverflow.com/questions/1101380/determine-if-running-on-a-rooted-device

To sum up; I have no advice for you to determine if device is rooted or not. But if I were you I would not use Runtime.getRuntime().exec().

By the way; RootTools.isRootAvailable() causes same problem.
The RootTools library offers simple methods to check for root:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章