Detecting device is rooted or not works fine on device but not on emulator

倾然丶 夕夏残阳落幕 提交于 2020-07-11 03:39:39

问题


I want to test whether device is rooted or not? For this I am using following code, this code works fine on device but on emulator it is showing it is rooted.

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;
 }
}

Is there is any other condition that should I have to check for device is rooted or not? Because i read online that there are various ways using that above code will not work. Any suggestion will be appreciated. Thanks in advance.

来源:https://stackoverflow.com/questions/30341034/detecting-device-is-rooted-or-not-works-fine-on-device-but-not-on-emulator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!