gpt4 book ai didi

Android 设备 root 检查

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:59 26 4
gpt4 key购买 nike

我想检查我的设备是否已获得 root 权限。当我在没有 root 的真实设备中尝试下面的代码时,没问题。但是非 root 模拟器在这一行中中断

if (new File(path).exists())
return true;

“/system/xbin/su”路径存在。

private static boolean isRooted() {
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;
}

Genymotion 或 Android studio 的模拟器总是在代码块中中断。

是否所有的 android 模拟器都已 root?

最佳答案

您可以通过以下方法检查设备是否已root:

public static boolean isRootedDevice(Context context) {

boolean rootedDevice = false;
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
Log.e("Root Detected", "1");
rootedDevice = true;
}

// check if /system/app/Superuser.apk is present
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
Log.e("Root Detected", "2");
rootedDevice = true;
}
} catch (Throwable e1) {
//Ignore
}

//check if SU command is executable or not
try {
Runtime.getRuntime().exec("su");
Log.e("Root Detected", "3");
rootedDevice = true;
} catch (IOException localIOException) {
//Ignore
}

//check weather busy box application is installed
String packageName = "stericson.busybox"; //Package for busy box app
PackageManager pm = context.getPackageManager();
try {
Log.e("Root Detected", "4");
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
rootedDevice = true;
} catch (PackageManager.NameNotFoundException e) {
//App not installed
}

return rootedDevice;
}

如果设备已获得 root 权限,它将返回 true,否则返回 false

关于Android 设备 root 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082905/

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