Делаю приложение с необходимостью рут доступа. Как мне можно сделать проверку того разрешил ли пользователь рут доступ моему приложению, или нет?
2 ответа
Самый простой и базовый пример определения 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");
}
// 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;
}
Для более продвинутого определения root-a посмотрите Android Root Beer.
думаю на английской части SO можно найти ответ. зависит от способа получения рута на устройстве. если есть su, то можно попробовать открыть файл/директорию за пределами каталога приложения. вот пример для изменения прав доступа (в моем случае для компорта) на Android-x86. но может он с косяками и надо доотладить.
static public boolean fixAndroidDeviceAccess(String path) {
File device = new File(path);
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod +rw " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if (su.waitFor() != 0)
{
return false;
}
} catch (Exception e) {
//e.printStackTrace();
Log.e(TAG, e.toString());
return false;
}
}
return device.canRead() && device.canWrite();
}