Android 应用检测设备是否被root
public class PropertyUtils {
private static final String ROOT_DEVICE = "0";
private static final String NON_ROOT_DEVICE = "1";
private static volatile Method get = null;
private static String getProperty(String prop, String defaultValue) {
String value;
try {
if (null == get) {
synchronized (PropertyUtils.class) {
if (null == get) {
Class<?> cls = Class.forName("android.os.SystemProperties");
get = cls.getDeclaredMethod("get", String.class, String.class);
}
}
}
value = (String) (get.invoke(null, prop, defaultValue));
} catch (Exception e) {
LogUtil.e("e: %s, PropertyUtils defaultValue: " + defaultValue);
return null;
}
return value;
}
public static boolean isRootDevice() {
return ROOT_DEVICE.equals(getProperty("ro.secure", NON_ROOT_DEVICE)) || new File("/system/bin/su").exists() || new File("/system/xbin/su").exists();
}
}
调用方法
if (PropertyUtils.isRootDevice()){
ToastUtil.show("设备已被root");
}else{
ToastUtil.show("设备未被root");
}
浙公网安备 33010602011771号