【Android】不弹root请求框检测手机是否root

2024-02-02 00:32

本文主要是介绍【Android】不弹root请求框检测手机是否root,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于项目需要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      * @return5      */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                 // error52                 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 result61             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行业人员进入!

这篇关于【Android】不弹root请求框检测手机是否root的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/668949

相关文章

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@