android - 确定是否在 Root设备上运行

标签 android root

我的应用程序具有某些功能,该功能仅适用于具有 root 权限的设备。与其在使用此功能时失败(然后向用户显示适当的错误消息),不如先静默检查 root 是否可用,如果没有,则首先隐藏相应的选项.

有没有办法做到这一点?

最佳答案

这是一个检查 Root 的三个方法之一。

/** @author Kevin Kowalewski */
public class RootUtil {
    public static boolean isDeviceRooted() {
        return checkRootMethod1() || checkRootMethod2() || checkRootMethod3();
    }

    private static boolean checkRootMethod1() {
        String buildTags = android.os.Build.TAGS;
        return buildTags != null && buildTags.contains("test-keys");
    }

    private static boolean checkRootMethod2() {
        String[] paths = { "/system/app/Superuser.apk", "/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", "/su/bin/su"};
        for (String path : paths) {
            if (new File(path).exists()) return true;
        }
        return false;
    }

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

关于android - 确定是否在 Root设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1101380/

相关文章:

java - 在 Android Studio 中添加外部库时出现问题

android - 按登录按钮时应用崩溃

Android:如何从我的 APK 中将文件系统挂载到 RW 中? (当然有根)

android - 如何在 root 的 android 设备上更改文件的组 ID?

linux - root 运行 cron 任务无法读取 www-data 用户生成的 .txt 文件

Android:如何在 root 设备上执行 chmod

PHP 根文件夹

android - 亚行可访问性重点变更

android - 在 Android 上使用 Room 时如何安装最新版本的 Sqlite aar

java - Sharedpreferences 在 SetText #android 时抛出空指针