【Android】不彈root請求框檢測手機是否root

来源:http://www.cnblogs.com/8hao/archive/2016/03/02/5235488.html
-Advertisement-
Play Games

由於項目需要root安裝軟體,並且希望在合適的時候引導用戶去開啟root安裝,故需要檢測手機是否root。 最基本的判斷如下,直接運行一個底層命令。(參考https://github.com/Trinea/android-common/blob/master/src/cn/trinea/androi


由於項目需要root安裝軟體,並且希望在合適的時候引導用戶去開啟root安裝,故需要檢測手機是否root。

最基本的判斷如下,直接運行一個底層命令。(參考https://github.com/Trinea/android-common/blob/master/src/cn/trinea/android/common/util/ShellUtils.java)

也可參考csdnhttp://blog.csdn.net/fm9333/article/details/12752415

複製代碼

  1     /**
  2      * check whether has root permission   3      *    4      * @return
  5      */
  6     public static boolean checkRootPermission() {   7         return execCommand("echo root", true, false).result == 0;   8     }   9     
 10 
 11     /**
 12      * execute shell commands  13      *   14      * @param commands  15      *            command array  16      * @param isRoot  17      *            whether need to run with root  18      * @param isNeedResultMsg  19      *            whether need result msg  20      * @return <ul>  21      *         <li>if isNeedResultMsg is false, {@link CommandResult#successMsg}  22      *         is null and {@link CommandResult#errorMsg} is null.</li>  23      *         <li>if {@link CommandResult#result} is -1, there maybe some  24      *         excepiton.</li>  25      *         </ul>  26      */
 27     public static CommandResult execCommand(String[] commands, boolean isRoot,  28             boolean isNeedResultMsg) {  29         int result = -1;  30         if (commands == null || commands.length == 0) {  31             return new CommandResult(result, null, null);  32         }  33 
 34         Process process = null;  35         BufferedReader successResult = null;  36         BufferedReader errorResult = null;  37         StringBuilder successMsg = null;  38         StringBuilder errorMsg = null;  39 
 40         DataOutputStream os = null;  41         try {  42             process = Runtime.getRuntime().exec(  43                     isRoot ? COMMAND_SU : COMMAND_SH);  44             os = new DataOutputStream(process.getOutputStream());  45             for (String command : commands) {  46                 if (command == null) {  47                     continue;  48                 }  49 
 50                 // donnot use os.writeBytes(commmand), avoid chinese charset  51                 // error
 52                 os.write(command.getBytes());  53                 os.writeBytes(COMMAND_LINE_END);  54                 os.flush();  55             }  56             os.writeBytes(COMMAND_EXIT);  57             os.flush();  58 
 59             result = process.waitFor();  60             // get command result
 61             if (isNeedResultMsg) {  62                 successMsg = new StringBuilder();  63                 errorMsg = new StringBuilder();  64                 successResult = new BufferedReader(new InputStreamReader(  65                         process.getInputStream()));  66                 errorResult = new BufferedReader(new InputStreamReader(  67                         process.getErrorStream()));  68                 String s;  69                 while ((s = successResult.readLine()) != null) {  70                     successMsg.append(s);  71                 }  72                 while ((s = errorResult.readLine()) != null) {  73                     errorMsg.append(s);  74                 }  75             }  76         } catch (IOException e) {  77             e.printStackTrace();  78         } catch (Exception e) {  79             e.printStackTrace();  80         } finally {  81             try {  82                 if (os != null) {  83                     os.close();  84                 }  85                 if (successResult != null) {  86                     successResult.close();  87                 }  88                 if (errorResult != null) {  89                     errorResult.close();  90                 }  91             } catch (IOException e) {  92                 e.printStackTrace();  93             }  94 
 95             if (process != null) {  96                 process.destroy();  97             }  98         }  99         return new CommandResult(result, successMsg == null ? null
100                 : successMsg.toString(), errorMsg == null ? null
101                 : errorMsg.toString()); 102     } 103 
104     /**
105      * result of command, 106      * <ul> 107      * <li>{@link CommandResult#result} means result of command, 0 means normal, 108      * else means error, same to excute in linux shell</li> 109      * <li>{@link CommandResult#successMsg} means success message of command 110      * result</li> 111      * <li>{@link CommandResult#errorMsg} means error message of command result</li> 112      * </ul> 113      *  114      * @author Trinea 2013-5-16 115      */
116     public static class CommandResult { 117 
118         /** result of command **/
119         public int result; 120         /** success message of command result **/
121         public String successMsg; 122         /** error message of command result **/
123         public String errorMsg; 124 
125         public CommandResult(int result) { 126             this.result = result; 127         } 128 
129         public CommandResult(int result, String successMsg, String errorMsg) { 130             this.result = result; 131             this.successMsg = successMsg; 132             this.errorMsg = errorMsg; 133         } 134     }    /**
135      * execute shell command, default return result msg 136      *  137      * @param command 138      *            command 139      * @param isRoot 140      *            whether need to run with root 141      * @return
142      * @see ShellUtils#execCommand(String[], boolean, boolean) 143      */
144     public static CommandResult execCommand(String command, boolean isRoot) { 145         return execCommand(new String[] { command }, isRoot, true); 146     }

複製代碼

但是這會帶來一個問題,每次判斷是否root都會彈出一個root請求框。這是十分不友好的一種交互方式,而且,用戶如果選擇取消,有部分手機是判斷為非root的。

這是方法一。交互不友好,而且有誤判。

在這個情況下,為了不彈出確認框,考慮到一般root手機都會有一些的特殊文件夾,比如/system/bin/su,/system/xbin/su,裡面存放有相關的許可權控制文件。

因此只要手機中有一個文件夾存在就判斷這個手機root了。

然後經過測試,這種方法在大部分手機都可行。

代碼如下:

複製代碼

 1     /** 判斷是否具有ROOT許可權 ,此方法對有些手機無效,比如小米系列 */
 2     public static boolean isRoot() {  3 
 4         boolean res = false;  5 
 6         try {  7             if ((!new File("/system/bin/su").exists())  8                     && (!new File("/system/xbin/su").exists())) {  9                 res = false; 10             } else { 11                 res = true; 12             } 13             ; 14         } catch (Exception e) { 15             res = false; 16         } 17         return res; 18     }

複製代碼

這是方法二。交互友好,但是有誤判。

後來測試的過程中發現部分國產,比如小米系列,有這個文件夾,但是系統是未root的,判斷成了已root。經過分析,這是由於小米有自身的許可權控制系統而導致。

考慮到小米手機有大量的用戶群,這個問題必須解決,所以不得不尋找第三種方案。

從原理著手,小米手機無論是否root,應該都是具有相關文件的。但是無效的原因應該是,文件設置了相關的許可權。導致用戶組無法執行相關文件。

從這個角度看,就可以從判斷文件的許可權入手。

先看下linux的文件許可權吧。

linux文件許可權詳細可參考《鳥叔的linux私房菜》http://vbird.dic.ksu.edu.tw/linux_basic/0210filepermission.php#filepermission_perm

只需要在第二種方法的基礎上,再另外判斷文件擁有者對這個文件是否具有可執行許可權(第4個字元的狀態),就基本可以確定手機是否root了。

在已root手機上(三星i9100 android 4.4),文件許可權(x或者s,s許可權,可參考http://blog.chinaunix.net/uid-20809581-id-3141879.html)如下

 

未root手機,大部分手機沒有這兩個文件夾,小米手機有這個文件夾。未root小米手機許可權如下(由於手頭暫時沒有小米手機,過幾天補上,或者有同學幫忙補上,那真是感激不盡)。

【等待補充圖片】

代碼如下:

複製代碼

 1     /** 判斷手機是否root,不彈出root請求框<br/> */
 2     public static boolean isRoot() {  3         String binPath = "/system/bin/su";  4         String xBinPath = "/system/xbin/su";  5         if (new File(binPath).exists() && isExecutable(binPath))  6             return true;  7         if (new File(xBinPath).exists() && isExecutable(xBinPath))  8             return true;  9         return false; 10     } 11 
12     private static boolean isExecutable(String filePath) { 13         Process p = null; 14         try { 15             p = Runtime.getRuntime().exec("ls -l " + filePath); 16             // 獲取返回內容
17             BufferedReader in = new BufferedReader(new InputStreamReader( 18                     p.getInputStream())); 19             String str = in.readLine(); 20             Log.i(TAG, str); 21             if (str != null && str.length() >= 4) { 22                 char flag = str.charAt(3); 23                 if (flag == 's' || flag == 'x') 24                     return true; 25             } 26         } catch (IOException e) { 27             e.printStackTrace(); 28         }finally{ 29             if(p!=null){ 30                 p.destroy(); 31             } 32         } 33         return false; 34     }

複製代碼

這種方法基本可以判斷所有的手機,而且不彈出root請求框。這才是我們需要的,perfect。

方法三,交互友好,基本沒有誤判。

以下是apk以及相關源代碼,大家可以下載apk看下運行效果

ROOT檢測APK下載地址:http://good.gd/3091610.htm

ROOT檢測代碼下載:http://good.gd/3091609.htm或者http://download.csdn.net/detail/waylife/7639017

如果有手機使用方法三無法判斷,歡迎提出。

也歡迎大家提出其他的更好的辦法。

問啊-定製化IT教育平臺牛人一對一服務,有問必答,開發編程社交頭條 官方網站:www.wenaaa.com

QQ群290551701 聚集很多互聯網精英,技術總監,架構師,項目經理!開源技術研究,歡迎業內人士,大牛及新手有志於從事IT行業人員進入!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Atitit.wrmi web rmi框架新特性 1. V1d 新特性1 1.1. 增加了精確參數1 1.2. 增加了req參數,命名參數模式。。1 1.3. 增加了globale 傳遞隱含參數req resp等1 1.4. Cs bs兩個版本的實現1 2. V2 新特性2 2.1. $req對象預
  • 協議 協議只有方法的聲明(類似於其他編程語言的介面) 協議相當於大家都所遵循的 關鍵字 @protocol 協議名 <所遵循的協議> 預設NSObject @end @protocollamcoProtocol <NSObject>@required //必須實現的方法 -(void)study;@
  • 本軟體設定用戶第一個接觸到的功能就是頁面載入等待功能,這個功能對使用者來說就是一個持續1、2秒鐘的等待頁面,在用戶等待的同時程式做一些必要的檢查以及數據準備工作,載入頁面分為UI篇和功能篇,從表及里首先是UI的實現,一個軟體除功能之外還得有一個光鮮的外表也是非常重要的,儘管本人設計水平一般但是還是親
  • 先看下onBackPressed和onKeyDown的區別 在Android上有兩種方法來獲取該按鈕的事件 1.直接獲取按鈕按下事件,此方法相容Android 1.0到Android 2.1 也是常規方法,直接重寫Activity的onKeyDown方法即可,代碼如下: @Override publ
  • 帶你走進游戲開發的世界之游戲幀動畫的處理<ignore_js_op> 1.幀動畫的原理 幀動畫幀動畫顧名思義,一幀一幀播放的動畫就是幀動畫。 幀動畫和我們小時候看的動畫片的原理是一樣的,在相同區域快速切換圖片給人們呈現一種視覺的假象感覺像是在播放動畫,其實不過是N張圖片在一幀一幀的切換罷了。 如圖所
  • 很多時候,AFNetworking都是目前iOS開發者網路庫中的不二選擇。Github上2W+的star數足見其流行程度。而從iOS7.0開始,蘋果推出了新的網路庫繼承者NSURLSession後,AFNetworking也毫不猶豫地加入了對其的支持。3.0+更加只是提供了NSURLSession的
  • 上一篇亂說了一陣socket,這篇要說說怎麼幹活了。畢竟用過的起來才行。 我的項目裡面使用的是CocoaAsyncSocket,這個是對CFSocket的封裝。如果你覺得自己可以實現封裝或者直接用原生的,我可以告訴你,很累;關鍵是等你弄出來,項目可能都要交了。這個庫,支持TCP和UDP;有GCD和R
  • - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ static BOOL showFlag = NO; if (!showFlag) { XZHomeViewController *home =
一周排行
    -Advertisement-
    Play Games
  • ## 引言 最近發現自己喜歡用的 Todo 軟體總是差點意思,畢竟每個人的習慣和工作流不太一樣,我就想著自己寫一個小的[Todo 項目]( https://github.com/circler3/TodoTrack ),核心的功能是自動記錄 Todo 執行過程中消耗的時間(尤其面向程式員),按照自己 ...
  • ### 前言 當我們編寫 C# 代碼時,經常需要處理大量的數據集合。在傳統的方式中,我們往往需要先將整個數據集合載入到記憶體中,然後再進行操作。但是如果數據集合非常大,這種方式就會導致記憶體占用過高,甚至可能導致程式崩潰。 C# 中的`yield return`機制可以幫助我們解決這個問題。通過使用`y ...
  • 1. ADO.NET的前世今生 ADO.NET的名稱起源於ADO(ActiveX Data Objects),是一個COM組件庫,用於在以往的Microsoft技術中訪問數據。之所以使用ADO.NET名稱,是因為Microsoft希望表明,這是在NET編程環境中優先使用的數據訪問介面。 ADO.NE ...
  • 1. 為什麼需要單元測試 在我們之前,測試某些功能是否能夠正常運行時,我們都將代碼寫到Main方法中,當我們測試第二個功能時,我們只能選擇將之前的代碼清掉,重新編寫。此時,如果你還想重新測試你之前的功能時,這時你就顯得有些難為情了,因為代碼都被你清掉了。當然你完全可以把代碼寫到一個記事本中進行記錄, ...
  • 1. 透過現象看本質 反射被譽為是 c#中的黑科技 ,在很多領域中都有反射的身影,例如,我們經常使用的ORM框架,ABP框架 等。 反射指程式可以訪問、檢測和修改它本身狀態或行為的一種能力。. 程式集包含模塊,而模塊包含類型,類型又包含成員。. 反射則提供了封裝程式集、模塊和類型的對象。. 您可以使 ...
  • # Rust Web 全棧開發之 Web Service 中的錯誤處理 ## Web Service 中的統一錯誤處理 ### Actix Web Service 自定義錯誤類型 -> 自定義錯誤轉為 HTTP Response - 資料庫 - 資料庫錯誤 - 串列化 - serde 錯誤 - I/ ...
  • 在前面的幾篇文章中,詳細地給大家介紹了Java里的集合。但在介紹集合時,我們涉及到了泛型的概念卻並沒有詳細學習,所以今天我們要花點時間給大家專門講解什麼是泛型、泛型的作用、用法、特點等內容 ...
  • ###BIO:同步阻塞 主線程發起io請求後,需要等待當前io操作完成,才能繼續執行。 ###NIO:同步非阻塞 引入selector、channel、等概念,當主線程發起io請求後,輪詢的查看系統是否準備好執行io操作,沒有準備好則主線程不會阻塞會繼續執行,準備好主線程會阻塞等待io操作完成。 # ...
  • 摘要:在讀多寫少的環境中,有沒有一種比ReadWriteLock更快的鎖呢?有,那就是JDK1.8中新增的StampedLock! 本文分享自華為雲社區《【高併發】高併發場景下一種比讀寫鎖更快的鎖》,作者: 冰 河。 什麼是StampedLock? ReadWriteLock鎖允許多個線程同時讀取共 ...
  • ## 併發與並行😣 ### 併發與並行的概念和區別 並行:同一個時間段內多個任務同時在不同的CPU核心上執行。強調同一時刻多個任務之間的”**同時執行**“。 併發:同一個時間段內多個任務都在進展。強調多個任務間的”**交替執行**“。 ![](https://img2023.cnblogs.co ...