首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >确定是否在根设备上运行

确定是否在根设备上运行
EN

Stack Overflow用户
提问于 2009-07-09 01:22:12
回答 17查看 211.1K关注 0票数 337

我的应用程序有一项特定的功能,只能在root可用的设备上工作。与其让这个特性在使用时失败(然后向用户显示适当的错误消息),我更喜欢先静默地检查root是否可用,如果不可用,则首先隐藏相应的选项。

有没有办法做到这一点?

EN

回答 17

Stack Overflow用户

发布于 2011-11-12 01:37:02

下面是一个类,它将通过以下三种方法之一来检查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();
        }
    }
}
票数 281
EN

Stack Overflow用户

发布于 2016-02-25 21:41:40

如果您已经在使用Fabric/Firebase Crashlytics,您可以调用

CommonUtils.isRooted(context)

下面是该方法的当前实现:

public static boolean isRooted(Context context) {
    boolean isEmulator = isEmulator(context);
    String buildTags = Build.TAGS;
    if (!isEmulator && buildTags != null && buildTags.contains("test-keys")) {
        return true;
    } else {
        File file = new File("/system/app/Superuser.apk");
        if (file.exists()) {
            return true;
        } else {
            file = new File("/system/xbin/su");
            return !isEmulator && file.exists();
        }
    }
}

public static boolean isEmulator(Context context) {
    String androidId = Secure.getString(context.getContentResolver(), "android_id");
    return "sdk".equals(Build.PRODUCT) || "google_sdk".equals(Build.PRODUCT) || androidId == null;
}
票数 85
EN

Stack Overflow用户

发布于 2011-07-03 22:57:34

RootTools库提供了检查根目录的简单方法:

RootTools.isRootAvailable()

Reference

票数 58
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1101380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档