确定Android设备是否以编程方式植根? [英] Determining if an Android device is rooted programmatically?
问题描述
可能重复:
确定是否在有根设备上运行
Possible Duplicate:
Determine if running on a rooted device
您如何(以编程方式)确定Android设备是否:已扎根运行软件或rom的破解副本.
How do you determine (programmatically) if an Android device is: rooted Running a cracked copy of your software or rom.
我的数据库中有一些敏感信息,我希望在手机扎根时对它进行加密,以使用户可以访问数据库.我该如何检测?
I have some sensitive information in my database, and I would like to encrypt it when the phone is rooted aka the user has access to the database. How do I detect that?
推荐答案
生根检测是一种猫捉老鼠的游戏,很难使生根检测在所有情况下都适用于所有设备.
Rooting detection is a cat and mouse game and it is hard to make rooting detection that will work on all devices for all cases.
请参见 Android根啤酒 https://github.com/scottyab/rootbeer用于高级根检测,它也使用JNI和编译到.so本机库中的本机CPP代码.
See Android Root Beer https://github.com/scottyab/rootbeer for advanced root detection which also uses JNI and native CPP code compiled into .so native library.
如果您需要一些简单和基础生根检测,请检查以下代码:
If you need some simple and basic rooting detection check the code below:
/**
* 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;
}
可能并不总是正确的. 2014年在约10种设备上进行了测试.
Probably not always correct. Tested on ~10 devices in 2014.
这篇关于确定Android设备是否以编程方式植根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!