判断手机是否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:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值