温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

两种方法检查Android是否已获取root权限

发布时间:2020-07-27 20:34:48 来源:网络 阅读:438 作者:阴阳光 栏目:移动开发

现在Android手机root权限可以说是轻松就获取得到的,而关于判断手机是否已经root的方法,你了解吗?如果app有一些特殊功能需要root权限,则需要判断是否root。不知道root是不是意味着手机不安全?说到底还是想一台究竟,你可以学习Android应用开发视频教程。下面介绍两种方法:

两种方法检查Android是否已获取root权限

方法1:

/**
* @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) {
}
}
}
}

ps:使用这些方法有可能将手机系统拖的非常慢,当应用多次启动后会创建出很多个僵死的进程耗用内存。


还有更多移动互联网教程资讯等请关注e良师益友网。



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI