gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:50:47 27 4
gpt4 key购买 nike

我的应用程序具有某些功能,该功能仅适用于具有 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com