Checks if the device is rooted.
判断手机是否被root
/**
* Checks if the device is rooted.
*
* @return <code>true</code> if the device is rooted, <code>false</code> otherwise.
*/
public static boolean isRooted() {
// get from build info
String buildTags = android.os.Build.TAGS;
if (buildTags != null && buildTags.contains("test-keys")) {
return true;
}
// check if /system/app/Superuser.apk is present
try {
File file = new File("/system/app/Superuser.apk");
if (file.exists()) {
return true;
}
} catch (Exception e1) {
// ignore
}
// try executing commands
return canExecuteCommand("/system/xbin/which su")
|| canExecuteCommand("/system/bin/which su") || canExecuteCommand("which su")
|| canExecuteCommand("busybox which su");
}
// executes a command on the system
private static boolean canExecuteCommand(String command) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String info = in.readLine();
if (info != null) return true;
return false;
} catch (Exception e) {
//do noting
} finally {
if (process != null) process.destroy();
}
return false;
}