判断手机是否root了

 1 public synchronized boolean getRootAhth()
 2         {
 3                 Process process = null;
 4                 DataOutputStream os = null;
 5                 try
 6                 {
 7                         process = Runtime.getRuntime().exec("su");
 8                         os = new DataOutputStream(process.getOutputStream());
 9                         os.writeBytes("exit\n");
10                         os.flush();
11                         int exitValue = process.waitFor();
12                         if (exitValue == 0)
13                         {
14                                 return true;
15                         } else
16                         {
17                                 return false;
18                         }
19                 } catch (Exception e)
20                 {
21                         Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "
22                                         + e.getMessage());
23                         return false;
24                 } finally
25                 {
26                         try
27                         {
28                                 if (os != null)
29                                 {
30                                         os.close();
31                                 }
32                                 process.destroy();
33                         } catch (Exception e)
34                         {
35                                 e.printStackTrace();
36                         }
37                 }
38         }

在运行程序的时候发现的问题,如果手机root过会弹出授权管理的对话框,如果选择允许exitValue = process.waitFor();就是0,判断不会有问题。如果选择拒绝或者不选择等倒计时结束exitValue = process.waitFor();值是255,就会判断没root。
所以判断条件要改一下if (exitValue == 0||exitValue == 255)。只在我手机上测了,不知道其他机器的值是不是一样,大家可以试一下,有不一样的贴出来。

posted on 2012-11-20 15:15  liyanqingyang  阅读(533)  评论(0编辑  收藏  举报

导航