Is it possible to programmatically say for sure if an android device is rooted?
I want to check if an android devices is rooted or not. If device is rooted I dont want my application to show the appropriate message to the user and the application should not work on a rooted device.
I have gone through various links and blogs which have code snipplets to check if device is rooted or not. But I also found multiple developers saying that it is not possible to programmatically check for sure if a device is rooted or no. The code snippets might not give 100% accurate results on all the devices and results might also depend on the tool used for rooting the android device.
Please let me know if there is any way to confirm for sure that the device is rooted or not programmatically.
Thanks, Sagar
I don't have enough reputation points to comment, so I have to add another answer.
The code in CodeMonkey's post works on most devices, but at least on Nexus 5 with Marshmallow it doesn't, because the which command actually works even on non-rooted devices. But because su doesn't work, it returns a non-zero exit value. This code expects an exception though, so it has to be modified like this:
private static boolean canExecuteCommand(String command) {
try {
int exitValue = Runtime.getRuntime().exec(command).waitFor();
return exitValue == 0;
} catch (Exception e) {
return false;
}
}
Possible duplicate of Stackoverflow.
This one has an answer
Answer on the second link. The guy tested it on about 10 devices and it worked for him.
/**
* 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");
}
// executes a command on the system
private static boolean canExecuteCommand(String command) {
boolean executedSuccesfully;
try {
Runtime.getRuntime().exec(command);
executedSuccesfully = true;
} catch (Exception e) {
executedSuccesfully = false;
}
return executedSuccesfully;
}
链接地址: http://www.djcxy.com/p/24612.html
上一篇: 过滤或隐藏可用的ChromeCast设备