问题
I want to check my device is rooted or not. When I try this code below in real device is not rooted, its ok. But Non rooted emulator break in this line
if (new File(path).exists())
return true;
"/system/xbin/su" path is exists.
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 or Android studio's emulator always break in code block.
Is all android emulators rooted?
回答1:
You can check the device is rooted or not by below method:
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;
}
It will return true if the device is rooted else false.
来源:https://stackoverflow.com/questions/53082905/android-device-rooted-check