android推断手机是否root
关于推断手机是否已经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的更多相关文章
- Android 判定手机是否root
Android获取手机root的状态 package com.app.demo; import java.io.File; import android.app.Activity; import an ...
- [Android] 安卓手机不用root删除自带app
声明:本文档仅供测试,因删除引起的一切问题,本人概不负责 声明:本文档仅供测试,因删除引起的一切问题,本人概不负责 声明:本文档仅供测试,因删除引起的一切问题,本人概不负责 转载请注明出处:https ...
- android 推断手机是否支持前置摄像头
话不多说 直接上代码, @SuppressLint("NewApi") public static boolean isSupportFrontCamera() { if (!ha ...
- 【Android】不弹root请求框检测手机是否root
由于项目需要root安装软件,并且希望在合适的时候引导用户去开启root安装,故需要检测手机是否root. 最基本的判断如下,直接运行一个底层命令.(参考https://github.com/Trin ...
- Android手机一键Root原理分析
图/文 非虫 一直以来,刷机与Root是Android手机爱好者最热衷的事情.即使国行手机的用户也不惜冒着失去保修的风险对Root手机乐此不疲.就在前天晚上,一年一度的Google I/O大会拉开了帷 ...
- 【转】Android检查手机是否被root
目前来说Android平台并没有提供能够root检查的工具.但是我们可以通过两种方式来判断 手机里面是否有su文件 这个su文件是不是能够执行 但是这两种检查方式都存在缺点. 第一种存在误测和漏测的情 ...
- Android使得手机拍照功能的发展(源共享)
Android系统调用手机拍照功能有两种方法来直接调用手机自带摄像头还有一个就是要当心自己的节拍. 例Camera360 强大的一个在每个操作系统都有一个手机摄影软件:您可以捕捉不同风格,不同特效的照 ...
- Android 获取手机信息
private void initData() { TelephonyManager mTm = (TelephonyManager) getActivity().getSystemService(C ...
- Android 上SuperUser获取ROOT权限原理解析
Android 上SuperUser获取ROOT权限原理解析 一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. ...
随机推荐
- C# 技巧收藏
IIS HTTP Error 500.21 - Internal Server Error 原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装 解决:开始- ...
- mysql server5.7 找不到my.ini,只有my-default.ini【mysql全局配置文件】
起因是在尝试将csv文件导入mysql的table时,出现如下错误: “The MySQL server is running with the --secure-file-priv option s ...
- 取代VS, sourceISight的IDE神器CLION
https://www.jetbrains.com/clion/download/download-thanks.html 随时升级 http://idea.lanyus.com/ m_pRemoti ...
- CCAction详解
http://blog.csdn.net/bailongvip/article/details/7895370 转载自雨松MOMO程序研究院 上一章我们了解了CCNode的实现原理,这次我跟大家探讨一 ...
- CGRectInset、CGRectOffset、等对比整理
http://blog.sina.com.cn/s/blog_76f3236b01013zmk.html 分类: iphone有关 1.CGRectInsetCGRect CGRectInset ...
- 编写COOL编译器
Coursera上面有Stanford的课程“Compilers”,该课程使用“龙书”作为参考书,并有一个编程项目,完成一个完整的编译器.这个编译器支持的语言称为COOL,是一个面向对象的用于教学的语 ...
- 《Linux命令、编辑器与shell编程》第三版 学习笔记---000
Linux概述 1.具有内核编程接口 2.支持多用户(同时) 3.支持多任务 4.支持安全的分层文件系统 a.标准 b.链接 c.权限 5.shell(命令解释器和编程语言) a.文件名生成(通配符和 ...
- C语言字节对齐问题详解【转】
引言 转自:http://www.cnblogs.com/clover-toeic/p/3853132.html 考虑下面的结构体定义: 1 typedef struct{ 2 char c1; 3 ...
- jquery做一个表单验证
正则表达式的写法: var re=new RegExp('规则', '可选参数');var re=/规则/参数;(这个最常用) 正则表达式的规则 和其他语言的正则表达式规则一样,可以参考我的另一边博文 ...
- centos6.x一键15项系统优化(转自努力哥)
#!/bin/sh ################################################ #Author:nulige # qqinfo: # Date: -- #vers ...