2012-11-19 85 views
5

我如何檢查Android設備是植根與否?我正在使用以下代碼:如何檢查android設備是否爲Rooted設備?

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

當我在設備上運行它時,出現以下異常。

Causes by:Permission denied 

但是,在模擬器上運行不會產生任何異常。

我正在使用另一種方式來檢查。在模擬器表彰行中輸入adb shell返回#,但對於設備寫入adb shell提供了以下錯誤:

[email protected]:/ $ su 
su 
/system/bin/sh: su: not found 
127|[email protected]:/ $ 

所以,我怎麼能檢查設備是否是植根與否。

在此先感謝。

回答

4

我使用這個類:

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"); 
      } 
    } 
相關問題