Android 是否 root

161 阅读1分钟
public static boolean isRooted(Context context) {
    if (TextUtils.equals("1", getSystemProperty("ro.secure", null))) {
        return true;
    }
    final String[] filePaths =
            new String[]{"/system/bin/su", "/system/sbin/su", "/system/xbin/su", "/vendor/bin/su", "/sbin/su"};
    for (String filePath : filePaths) {
        final File file = new File(filePath);
        if (file.exists()) {
            return true;
        }
    }
    return false;
}

@Nullable
public static String getSystemProperty(String name, String defaultValue) {
    if (TextUtils.isEmpty(name)) {
        return defaultValue;
    }
    try {
        final Class<?> cls = Class.forName("android.os.SystemProperties");
        final Method method = cls.getDeclaredMethod("get", String.class, String.class);
        return (String) method.invoke(null, name, defaultValue);
    } catch (Throwable th) {
        return defaultValue;
    }
}