欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 检测手机是否被Root

程序员文章站 2022-05-10 09:42:44
...
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();
    }
}

 

相关标签: root