检测设备是否已植根或无法在设备上正常工作但在仿真器上无效

时间:2015-05-20 05:33:13

标签: android root

我想测试设备是否已植根?为此,我使用以下代码,此代码在设备上正常工作,但在模拟器上它显示它是root。

public class RootUtil {

public static boolean isDeviceRooted() {
    return  (checkRootMethod1()||checkRootMethod2() || checkRootMethod3()|| checkRootMethod4()||checkRootMethod5()) ;
}

private static boolean checkRootMethod1() {
    System.out.println ("Inside checkRootMethod1");
    String buildTags = android.os.Build.TAGS;

    if (buildTags != null && buildTags.contains("test-keys")) {
        System.out.println ("Inside if of 1");
        return true;
    }
    return false;
}

private static boolean checkRootMethod2() {
    System.out.println("checkRootMethod2"+new File("/system/app/Superuser.apk").exists());
    return new File("/system/app/Superuser.apk").exists();
}

private static boolean checkRootMethod3() {
    String[] paths = { "/sbin/su", "/system/bin/su", "/system/xbin/su", "/data/local/xbin/su", "/data/local/bin/su", "/system/sd/xbin/su",
            "/system/bin/failsafe/su", "/data/local/su" };
    for (String path : paths) {
        System.out.println("checkRootMethod3"+new File(path).exists());
        if (new File(path).exists()) return true;
    }
    return false;
}

private static boolean checkRootMethod4() {
    Process process = null;
    try {
        process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        System.out.println("checkRootMethod4"+in.readLine() != null);
        if (in.readLine() != null) return true;
        return false;
    } catch (Throwable t) {
        return false;
    } finally {
        if (process != null) process.destroy();
    }
}

private static boolean checkRootMethod5() {
    return canExecuteCommand("/system/xbin/which su")
            || canExecuteCommand("/system/bin/which su")
            || canExecuteCommand("which su");
}

private static boolean canExecuteCommand(String command) {
    boolean executedSuccesfully;
    try {
        Runtime.getRuntime().exec(command);
        executedSuccesfully = true;
    } catch (Exception e) {
        executedSuccesfully = false;
    }

    return executedSuccesfully;
 }
}

是否还有其他条件需要检查设备是否已植根?因为我在网上看到有各种方法使用上面的代码将无法正常工作。任何建议将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

查看库RootTools 4.2它非常适合检查root和其他root用户。

https://github.com/Stericson/RootTools/releases