关于推断手机是否已经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. PHP会话控制

         会话控制的思想是指能够在网站中根据一个会话跟踪用户.      PHP的会话是通过唯一的会话ID来驱动的.会话ID是一个加密的随机数字.它由PHP生成,在会话的生命周期中都会保存在客户端.它 ...

  2. OpenGL入门学习(四)

    http://blog.csdn.net/sun6255028/article/details/5090055 OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式.无论哪种颜色模式,计算 ...

  3. Android SQLite使用

    1. 介绍 SQLite是一款轻型的数据库, 是遵守ACID的关系型数据库管理系统, Android系统已经在框架中适配接口供用户使用. 2. 数据类型 SQLite采用的是动态数据类型, 会根据存入 ...

  4. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  5. linux内核情景分析之exit与Wait

    //第一层系统调用 asmlinkage long sys_exit(int error_code) { do_exit((error_code&0xff)<<8); } 其主体是 ...

  6. linux内存管理2:内存映射和需求分页(英文名字:demand Paging,又叫:缺页中断)【转】

    转自:http://blog.csdn.net/zhangxinrun/article/details/5873148 当某个程序映象开始运行时,可执行映象必须装入进程的虚拟地址空间.如果该程序用到了 ...

  7. Git 的使用教程

    Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. 安装Git 1.方法一:安装homebrew,然后通过homebrew安装Git. 第一步:在终端输入如下命令 ...

  8. 3.Centos-Docker-rancher

    1.安装mysql,设置密码 docker run -d --name mysqldb -e MYSQL_ROOT_PASSWORD=密码 mysql:latest --character-set-s ...

  9. 使用windos电脑模拟搭建web集群(一)

    资源规划 1.环境准备 centos7.2 虚拟机 13个 可以先配置一台,做好基础优化,然后克隆13台分布在 windos宿主机上 两台windos笔记  都是8g的内存 一台有点内存吃紧. 没有物 ...

  10. printf()函数不能直接输出string类型

    因为string不是c语言的内置数据,所以直接printf输出string类型的是办不到的. 要这样输出: printf("%s\n",a.c_str()); 举例: #inclu ...