如何检查安卓设备是否已经取得Root权限?

5

我该如何检查安卓设备是否已经取得了root权限?我正在使用以下代码:

Process proc = Runtime.getRuntime ().exec ("su");

当我在设备上运行它时,出现了以下异常。
Causes by:Permission denied

但在模拟器上运行时没有出现任何异常。

我使用另一种方法来检查。在模拟器的命令行中输入adb shell 返回 #,但对于设备,写入 adb shell 会出现以下错误:

shell@android:/ $ su  
su   
/system/bin/sh: su: not found   
127|shell@android:/ $  

那么如何检查设备是否已经被root了呢?

谢谢提前。

1个回答

5

我使用这个类:

private void CheckRoot()
    {                    
        Process p;

        try {  
               // Preform su to get root privledges  
               p = Runtime.getRuntime().exec("su");   

               // Attempt to write a file to a root-only  
               DataOutputStream os = new DataOutputStream(p.getOutputStream());  
               os.writeBytes("echo \"Do I have root?\" >/data/LandeRootCheck.txt\n");  

               // Close the terminal  
               os.writeBytes("exit\n");  
               os.flush();  
               try {  
                  p.waitFor();  
                       if (p.exitValue() == 0) {  
                          // TODO Code to run on success                         
                         this.IsRoot=true;
                       }  
                       else {  
                           // TODO Code to run on unsuccessful  
                           this.IsRoot=false;  
                       }  
               } catch (InterruptedException e) {  
                  // TODO Code to run in interrupted exception  
                   toastMessage("not root");  
               }  
            } catch (IOException e) {  
               // TODO Code to run in input/output exception  
                toastMessage("not root");  
            }  
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接