关于推断手机是否已经root的方法。假设app有一些特殊功能须要root权限,则须要推断是否root。

比方一些市场下载完app后自己主动安装。

/**
* @author Kevin Kowalewski
*
*/
public class Root { private static String LOG_TAG = Root.class.getName(); public boolean isDeviceRooted() {
if (checkRootMethod1()){return true;}
if (checkRootMethod2()){return true;}
if (checkRootMethod3()){return true;}
return false;
} public boolean checkRootMethod1(){
String buildTags = android.os.Build.TAGS; if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
return false;
} public boolean checkRootMethod2(){
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
} catch (Exception e) { } return false;
} public boolean checkRootMethod3() {
if (new ExecShell().executeCommand(SHELL_CMD.check_su_binary) != null){
return true;
}else{
return false;
}
}
} /**
* @author Kevin Kowalewski
*
*/
public class ExecShell { private static String LOG_TAG = ExecShell.class.getName(); public static enum SHELL_CMD {
check_su_binary(new String[] {"/system/xbin/which","su"}),
; String[] command; SHELL_CMD(String[] command){
this.command = command;
}
} public ArrayList<String> executeCommand(SHELL_CMD shellCmd){
String line = null;
ArrayList<String> fullResponse = new ArrayList<String>();
Process localProcess = null; try {
localProcess = Runtime.getRuntime().exec(shellCmd.command);
} catch (Exception e) {
return null;
//e.printStackTrace();
} BufferedWriter out = new BufferedWriter(new OutputStreamWriter(localProcess.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(localProcess.getInputStream())); try {
while ((line = in.readLine()) != null) {
Log.d(LOG_TAG, "--> Line received: " + line);
fullResponse.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} Log.d(LOG_TAG, "--> Full response was: " + fullResponse); return fullResponse;
} }

代码来自stackoverflow。向作者致敬。

方法2:

The RootTools library offers simple methods to check for root:

一个开源项目:http://code.google.com/p/roottools/

RootTools.isRootAvailable()推断是否root

RootTools.isAccessGiven()返回true那么手机已经root而且app也被授予root权限。

另外:据那片帖子的一个回贴人说使用

String commandToExecute = "su";
executeShellCommand(commandToExecute);
private boolean executeShellCommand(String command){
Process process = null;
try{
process = Runtime.getRuntime().exec(command);
return true;
} catch (Exception e) {
return false;
} finally{
if(process != null){
try{
process.destroy();
}catch (Exception e) {
}
}
}
}

会引起很严重的性能问题,将手机系统拖的很慢,当应用多次启动后会创建出许多个僵死的进程耗用内存。

參考http://stackoverflow.com/questions/1101380/determine-if-running-on-a-rooted-device

To sum up; I have no advice for you to determine if device is rooted or not. But if I were you I would not use Runtime.getRuntime().exec().

By the way; RootTools.isRootAvailable() causes same problem.

The RootTools library offers simple methods to check for root:

android推断手机是否root的更多相关文章

  1. Android 判定手机是否root

    Android获取手机root的状态 package com.app.demo; import java.io.File; import android.app.Activity; import an ...

  2. [Android] 安卓手机不用root删除自带app

    声明:本文档仅供测试,因删除引起的一切问题,本人概不负责 声明:本文档仅供测试,因删除引起的一切问题,本人概不负责 声明:本文档仅供测试,因删除引起的一切问题,本人概不负责 转载请注明出处:https ...

  3. android 推断手机是否支持前置摄像头

    话不多说 直接上代码, @SuppressLint("NewApi") public static boolean isSupportFrontCamera() { if (!ha ...

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

    由于项目需要root安装软件,并且希望在合适的时候引导用户去开启root安装,故需要检测手机是否root. 最基本的判断如下,直接运行一个底层命令.(参考https://github.com/Trin ...

  5. Android手机一键Root原理分析

    图/文 非虫 一直以来,刷机与Root是Android手机爱好者最热衷的事情.即使国行手机的用户也不惜冒着失去保修的风险对Root手机乐此不疲.就在前天晚上,一年一度的Google I/O大会拉开了帷 ...

  6. 【转】Android检查手机是否被root

    目前来说Android平台并没有提供能够root检查的工具.但是我们可以通过两种方式来判断 手机里面是否有su文件 这个su文件是不是能够执行 但是这两种检查方式都存在缺点. 第一种存在误测和漏测的情 ...

  7. Android使得手机拍照功能的发展(源共享)

    Android系统调用手机拍照功能有两种方法来直接调用手机自带摄像头还有一个就是要当心自己的节拍. 例Camera360 强大的一个在每个操作系统都有一个手机摄影软件:您可以捕捉不同风格,不同特效的照 ...

  8. Android 获取手机信息

    private void initData() { TelephonyManager mTm = (TelephonyManager) getActivity().getSystemService(C ...

  9. Android 上SuperUser获取ROOT权限原理解析

    Android 上SuperUser获取ROOT权限原理解析 一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. ...

随机推荐

  1. [摸鱼] 配置的vim的使用攻略!

    vim使用攻略 <>=f 折叠与缩进 开:<>[^fuck]cc 关:<>cu 缩进一块使用V选中,按>> [Ctrl]V 以列为单位选 za,打开或关 ...

  2. SQL 整理

    批量插入 insert into table select ... union all select... insert into table (...) values (...) , (...) i ...

  3. glEnable(GL_DEPTH_TEST)的问题

    http://www.gameres.com/msg_195903.html 在程序中加入glEnable(GL_DEPTH_TEST)之后显示就完全黑屏了,即使是清空了深度缓冲glClear(GL_ ...

  4. python enumerate元素的时候可以获取下标,并且可以指定开始的下标值。

    list=["a","b","c","d","e"] for i,item in enumerate ...

  5. windows 添加自助白名单

    由于公司分部用的是动态IP,又需要用到总部的OA系统,OA完全开放对外不安全,所以写了这个工具 项目地址 https://github.com/cainiaoit/Windows-firewall-s ...

  6. Vim文字编辑

    首先说明发现的vim编辑器的一个特点:vim编辑只有按[ENTER]键或命令模式下[o]才会换行,否则虽然在vim编辑器里显示的内容换行了,但事实上没有换行.如果你发现自己测试的效果和下面描述的不符, ...

  7. Flask插件系列之flask_celery

    现在继续学习在集成的框架中如何使用celery. 在Flask中使用celery 在Flask中集成celery需要做到两点: 创建celery的实例对象的名字必须是flask应用程序app的名字,否 ...

  8. (8)oracle 表的增删改

    表的命名 表需要字母开头 只能用如下字符 A-Z,a-z,0-9,$,#. 不能使用oracle保留字 长度不能超过30 创建一张表 create table 表名(字段名 数据类型,字段名 数据类型 ...

  9. Python的程序结构[4] -> 函数/Function[1] -> 内建函数

    内建函数 / Built-in Function or Method Python中有许多的内建函数(查看内建模块部分),此处将对内建函数进行介绍 内建函数 ord / built-in functi ...

  10. hashmap hashtable

    作者:付佳豪链接:https://zhuanlan.zhihu.com/p/37607299来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 在面试的时候,java集合最 ...