Android 检测设备是否root

本文主要记录下android 中检测设备是否root的常规方法:

  1. 检查系统目录:在root过的设备上,系统目录下会存在一些特殊的文件或目录,如"/system/bin/su"、"/system/xbin/su"等。可以通过检查这些文件或目录是否存在来判断设备是否已经root。
  2. 通过执行su命令.

package com.test.detection;

import java.io.DataOutputStream;
import java.io.File;

/**

  • @Author: zh

  • @Time: 23-11-28.

  • @Email:

  • @Describe:检测是否root
    */
    public class RootUtils {

    public static boolean isDeviceRooted2() {
    String buildTags = android.os.Build.TAGS;
    if (buildTags != null && buildTags.contains(“test-keys”)) {
    return true;
    }
    File file = new File(“/system/app/Superuser.apk”);
    if (file.exists()) {
    return true;
    }
    String su = “su”;
    String[] locations = {“/sbin/”, “/system/bin/”, “/system/xbin/”, “/system/sd/xbin/”, “/system/bin/failsafe/”, “/data/local/xbin/”, “/data/local/bin/”, “/data/local/”};
    for (String location : locations) {
    if (new File(location + su).exists()) {
    return true;
    }
    }
    return false;
    }

    public static boolean isDeviceRooted(){
    Process process = null;
    try {
    process = Runtime.getRuntime().exec(“su”);
    DataOutputStream os = new DataOutputStream(process.getOutputStream());
    os.writeBytes(“exit\n”);
    os.flush();
    int exitValue = process.waitFor();
    return exitValue == 0;
    } catch (Exception e) {
    // 应用未获取root权限
    } finally {
    if (process != null) {
    process.destroy();
    }
    }
    return false;
    }

}

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值