【Android】不弹root请求框检测手机是否root
由于项目需要root安装软件,并且希望在合适的时候引导用户去开启root安装,故需要检测手机是否root。
最基本的判断如下,直接运行一个底层命令。(参考https://github.com/Trinea/android-common/blob/master/src/cn/trinea/android/common/util/ShellUtils.java)
也可参考csdn http://blog.csdn.net/fm9333/article/details/12752415
/**
* check whether has root permission
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
} /**
* execute shell commands
*
* @param commands
* command array
* @param isRoot
* whether need to run with root
* @param isNeedResultMsg
* whether need result msg
* @return <ul>
* <li>if isNeedResultMsg is false, {@link CommandResult#successMsg}
* is null and {@link CommandResult#errorMsg} is null.</li>
* <li>if {@link CommandResult#result} is -1, there maybe some
* excepiton.</li>
* </ul>
*/
public static CommandResult execCommand(String[] commands, boolean isRoot,
boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
} Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null; DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(
isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
} // donnot use os.writeBytes(commmand), avoid chinese charset
// error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush(); result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
} if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null
: successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
} /**
* result of command,
* <ul>
* <li>{@link CommandResult#result} means result of command, 0 means normal,
* else means error, same to excute in linux shell</li>
* <li>{@link CommandResult#successMsg} means success message of command
* result</li>
* <li>{@link CommandResult#errorMsg} means error message of command result</li>
* </ul>
*
* @author Trinea 2013-5-16
*/
public static class CommandResult { /** result of command **/
public int result;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg; public CommandResult(int result) {
this.result = result;
} public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
} /**
* execute shell command, default return result msg
*
* @param command
* command
* @param isRoot
* whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] { command }, isRoot, true);
}
但是这会带来一个问题,每次判断是否root都会弹出一个root请求框。这是十分不友好的一种交互方式,而且,用户如果选择取消,有部分手机是判断为非root的。
这是方法一。交互不友好,而且有误判。
在这个情况下,为了不弹出确认框,考虑到一般root手机都会有一些的特殊文件夹,比如/system/bin/su,/system/xbin/su,里面存放有相关的权限控制文件。
因此只要手机中有一个文件夹存在就判断这个手机root了。
然后经过测试,这种方法在大部分手机都可行。
代码如下:
/** 判断是否具有ROOT权限 ,此方法对有些手机无效,比如小米系列 */
public static boolean isRoot() { boolean res = false; try {
if ((!new File("/system/bin/su").exists())
&& (!new File("/system/xbin/su").exists())) {
res = false;
} else {
res = true;
}
;
} catch (Exception e) {
res = false;
}
return res;
}
这是方法二。交互友好,但是有误判。
后来测试的过程中发现部分国产,比如小米系列,有这个文件夹,但是系统是未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小米手机权限如下(由于手头暂时没有小米手机,过几天补上,或者有同学帮忙补上,那真是感激不尽)。
【等待补充图片】
代码如下:
/** 判断手机是否root,不弹出root请求框<br/> */
public static boolean isRoot() {
String binPath = "/system/bin/su";
String xBinPath = "/system/xbin/su";
if (new File(binPath).exists() && isExecutable(binPath))
return true;
if (new File(xBinPath).exists() && isExecutable(xBinPath))
return true;
return false;
} private static boolean isExecutable(String filePath) {
Process p = null;
try {
p = Runtime.getRuntime().exec("ls -l " + filePath);
// 获取返回内容
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String str = in.readLine();
Log.i(TAG, str);
if (str != null && str.length() >= 4) {
char flag = str.charAt(3);
if (flag == 's' || flag == 'x')
return true;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(p!=null){
p.destroy();
}
}
return false;
}
这种方法基本可以判断所有的手机,而且不弹出root请求框。这才是我们需要的,perfect。
方法三,交互友好,基本没有误判。
以下是apk以及相关源代码,大家可以下载apk看下运行效果
ROOT检测APK下载地址:http://good.gd/3091610.htm
ROOT检测代码下载:http://good.gd/3091609.htm或者http://download.csdn.net/detail/waylife/7639017
如果有手机使用方法三无法判断,欢迎提出。
也欢迎大家提出其他的更好的办法。
【Android】不弹root请求框检测手机是否root的更多相关文章
- android 透明弹出搜索框
1.在QQ一些APP当中有是弹出一个半透明的搜索框的,其实这种效果就是很多种方法,自定义一个Dialog,或者直接将activity的背景变成半透明的也可以的. 下面就是将activity变成半透明的 ...
- Android手机一键Root原理分析
图/文 非虫 一直以来,刷机与Root是Android手机爱好者最热衷的事情.即使国行手机的用户也不惜冒着失去保修的风险对Root手机乐此不疲.就在前天晚上,一年一度的Google I/O大会拉开了帷 ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- 禁止手机页面中A标签长按弹出路径框
//禁止手机页面中A标签长按弹出路径框 window.onload=function(){ document.documentElement.style.webkitTouchCa ...
- Android弹出选项框及指示箭头动画选择
Android弹出选项框及指示箭头动画选择 Android原生的Spinner提供了下拉列表选项框,但在一些流行的APP中,原生的Spinner似乎不太受待见,而通常会有下图所示的下拉列表选项框 ...
- Android Studio 打开弹出警告框
1.Android Studio打开后,自己的项目没有打开,就弹出了警告框,重启之后依然弹出警告框: 警告框内容:"Cannot load project: java.lang.Illega ...
- Cocos2d-x C++调用Android弹出提示框
转载请注明地址,谢谢.. Cocos2d-x中提供了一个JniHelper类来让我们对Jni进行操作. (PS:弄了一天想自己写代码操作Jni的,但是总是出错,技术差不得不使用Cocos2d-x现成的 ...
- Ajax发送请求等待时弹出模态框等待提示
主要的代码分为两块,一个是CSS定义模态框,另一个是在Ajax中弹出模态框. 查看菜鸟教程中的模态框教程demo,http://www.runoob.com/try/try.php?filename= ...
随机推荐
- 540C: Ice Cave
题目链接 题意: n*m的地图,'X'表示有裂痕的冰块,'.'表示完整的冰块,有裂痕的冰块再被踩一次就会碎掉,完整的冰块被踩一次会变成有裂痕的冰块, 现在告诉起点和终点,问从起点能否走到终点并且使终点 ...
- Android核心分析之二十二Android应用框架之Activity
3 Activity设计框架 3.1 外特性空间的Activity 我们先来看看,android应用开发人员接触的外特性空间中的Activity,对于AMS来讲,这个Activity就是客服端的 ...
- SQLHelper.cs的经典代码-存储过程
using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...
- MDK5.01百度云下载,安装微软雅黑混合字体,字体效果很棒,解决显示中文的BUG
微软雅黑字体http://pan.baidu.com/s/1nt9Epuh 初步尝试,以前的小BUG都已经解决了.下面是安装雅黑字体后的字体效果,很爽.第一步:安装雅黑字体.第二步:选择Edit--- ...
- 物联网操作系统Hello China移植mile stone之一:移植基础版本V1.76发布
Hello China V1.76版发布,这是向ARM系列CPU移植的基础版本.相对V1.75版,该版本主要做了如下的一些调整: 1. 通过宏定义的方式对内核实现了模块化,开发者可以通过开启或关闭预 ...
- android调试工具DDMS
DDMS工作机制 DDMS全称Dalvik Debug Monitor Service.DDMS为IDE和emultor及真正的android设备架起来了一座桥梁,Android DDMS将捕捉 ...
- CentOS配置SSH单向无密码访问
最近在研究一款文件系统,需要远程给客户机安装软件,且需要无SSH密码访问,另外需要远程给客户机传文件,每次输入root密码很不方便,就想到用ssh key生成公钥.私钥来验证,而避免每次就必须输入ro ...
- (CentOS) 程序安装包管理:yum
简介: Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器 ...
- skip-name-resolv
skip-name-resolve skip-name-resolve 简单解释 MySQL server received a request from you to allow you to co ...
- dwz ie10一直提示数据加载中
dwz js资源jquery.validate.js 搜索 this.attr('novalidate', 'novalidate'); 在33行左右 用if (typeof (Worker) !== ...