5

How can i check if android device is rooted or not? I am using the following code:

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

and when I run it on a device I got following exception.

Causes by:Permission denied

But running on an emulator does not give any exception.

I am using another way to check. Entering adb shell in the commend line for emulator returns #, but for device writing adb shell gives the following error:

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

So how can I check if device is rooted or not.

Thanks in Advance.

Lokesh Mehra
  • 545
  • 6
  • 16

1 Answers1

5

I use this class:

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");  
            }  
    }
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82