Android 判断手机是否root

//判断手机是否root
public static boolean isRoot() {
    String binPath = "/system/bin/su";
    String xBinPath = "/system/xbin/su";
 
    if (new File(binPath).exists() && isCanExecute(binPath)) {
        return true;
    }
    if (new File(xBinPath).exists() && isCanExecute(xBinPath)) {
        return true;
    }
    return false;
}
 
private static boolean isCanExecute(String filePath) {
    java.lang.Process process = null;
    try {
        process = Runtime.getRuntime().exec("ls -l " + filePath);
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String str = in.readLine();
        if (str != null && str.length() >= 4) {
            char flag = str.charAt(3);
            if (flag == 's' || flag == 'x')
                return true;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return false;
}             

猜你喜欢

转载自blog.csdn.net/xlwang9090/article/details/80843453