android 检测手机是否被Root

import android.text.TextUtils;


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

/**
 * 使用 boolean isRoot = PACheckSysUtils.isRoot();
 * 检测手机是否被Root
 */
public class CheckSysUtils {
    private static final String TAG = "PACheckSysUtils";

    /**
     * 检测是否root
     * modify by hiyi 20180928,移除雁联SDK的root检测,重新开发
     *
     * @param
     * @return
     */
    public static boolean isRoot() {
        String[] paths = {
                "/system/xbin/su",
                "/system/bin/su",
                "/system/sbin/su",
                "/sbin/su",
                "/vendor/bin/su",
                "/su/bin/su"
        };
        try {
            for (String path : paths) {
                if (new File(path).exists()) {
                    String execResult = exec(new String[]{"ls", "-l", path});
                    Log.d(TAG, "isRooted = " + execResult);
                    //形如(rooted):-rwxr-xr-x root     root        75348 1970-01-01 08:32 su
                    if (TextUtils.isEmpty(execResult)
                            || execResult.indexOf("root") == execResult.lastIndexOf("root")) {
                        return false;
                    }
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static String exec(String[] exec) {
        if (exec == null || exec.length <= 0) {
            return null;
        }
        StringBuilder ret = new StringBuilder();
        ProcessBuilder processBuilder = new ProcessBuilder(exec);
        try {
            Process process = processBuilder.start();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                ret.append(line);
            }
            process.getInputStream().close();
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret.toString();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值