要检查Android设备是否已经root,可以使用以下代码示例来实现:
public boolean isDeviceRooted() {
return checkRootMethod1() || checkRootMethod2() || checkRootMethod3();
}
private boolean checkRootMethod1() {
String buildTags = android.os.Build.TAGS;
return buildTags != null && buildTags.contains("test-keys");
}
private 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" };
for (String path : paths) {
if (new File(path).exists()) {
return true;
}
}
return false;
}
private boolean checkRootMethod3() {
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
return in.readLine() != null;
} catch (Throwable t) {
return false;
} finally {
if (process != null) {
process.destroy();
}
}
}
这个方法通过检查一些与root有关的标志、文件路径和命令来判断设备是否已root。如果任意一个检查方法返回true,则认为设备已root。
请注意,这个方法并不是绝对可靠的,因为root检查是一场持续的猫鼠游戏。