Android 應用檢測裝置是否被root

白衣雨果發表於2024-11-18
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");
 }

  

相關文章