三方应用如何使用反射获取SystemProperties

public class GetSystemProperties {
    private static Class<?> systemProperties() throws Exception {
        return Class.forName("android.os.SystemProperties");
    }

    public static String get(String key) {
        try {
            final Class<?> SystemProperties = systemProperties();
            return (String) SystemProperties.getMethod("get", new Class[]{String.class})
                    .invoke(SystemProperties, key);
        } catch (Exception e) {
            return "";
        }
    }

    public static String get(String key, String def) {
        try {
            final Class<?> SystemProperties = systemProperties();
            return (String) SystemProperties.getMethod("get", new Class[]{String.class, String.class})
                    .invoke(SystemProperties, key, def);
        } catch (Exception e) {
            return def;
        }
    }

    public static int getInt(String key, int def) {
        try {
            final Class<?> SystemProperties = systemProperties();
            return (int) SystemProperties.getMethod("getInt", new Class[]{String.class, int.class})
                    .invoke(SystemProperties, key, def);
        } catch (Exception e) {
            return def;
        }
    }

    public static long getLong(String key, long def) {
        try {
            final Class<?> SystemProperties = systemProperties();
            return (long) SystemProperties.getMethod("getLong", new Class[]{String.class, long.class})
                    .invoke(SystemProperties, key, def);
        } catch (Exception e) {
            return def;
        }
    }

    public static boolean getBoolean(String key, boolean def) {
        try {
            final Class<?> SystemProperties = systemProperties();
            return (boolean) SystemProperties.getMethod("getBoolean", new Class[]{String.class, boolean.class})
                    .invoke(SystemProperties, key, def);
        } catch (Exception e) {
            return def;
        }
    }

    public static void set(String key, String val) {
        try {
            final Class<?> SystemProperties = systemProperties();
            SystemProperties.getMethod("set", new Class[]{String.class, String.class})
                    .invoke(SystemProperties, key, val);
        } catch (Exception ignored) {}
    }

    public static void addChangeCallback(Runnable callback) {
        try {
            final Class<?> SystemProperties = systemProperties();
            SystemProperties.getMethod("addChangeCallback", new Class[]{Runnable.class})
                    .invoke(SystemProperties, callback);
        } catch (Exception ignored) {}
    }
}

当然,若不分开写,也完全可以只写set & get方法:

import java.lang.reflect.Method;

/*
  Class using reflection to grant access to the private hidden android.os.SystemProperties class
 */
public class SystemPropertiesReflection {

    protected static final String GET_METHOD = "get";
    protected static final String SET_METHOD = "set";

    /**
     * This class cannot be instantiated
     */
    private SystemPropertiesReflection() {

    }

    /**
     * Get the value for the given key, and returns as a String
     *
     * @param key Key of the system property
     * @return value for the given key or null if: the key isn't found or an error occurred
     */
    public static String get(String key) {
        return getSystemPropertyValue(key,null);
    }

    /**
     * Get the value for the given key.
     *
     * @param key Key of the system property
     * @param def default value to return if the system property is not found
     * @return value for the given key or null if: the key isn't found or an error occurred
     */
    public static String get(String key, String def) {
        return getSystemPropertyValue(key,def);
    }

    /**
     * Get the system property value for the given key.
     * @param key Key of the system property
     * @param def default value to return if the system property is not found
     * @return value for the given key or null if: the key isn't found or an error occurred
     */
    protected static String getSystemPropertyValue(String key, String def) {
        String ret = null;

        try {
            // Get the class object
            Class systemProperties = Class.forName("android.os.SystemProperties");


            if (null == def) {
                // Retrieve the method
                Method get = systemProperties.getMethod(GET_METHOD, String.class);

                // Invoke the get method
                ret = (String) get.invoke(null, key);
            } else {
                // Retrieve the method
                Method get = systemProperties.getMethod(GET_METHOD, String.class, String.class);

                // Invoke the get method
                ret = (String) get.invoke(null, key, def);
            }

            if ((null != ret) && (ret.isEmpty())) {
                // In case of empty string returning null
                ret = null;
            }
        } catch (Exception e) {
            // We ignore any exception ad return null
        }
        return ret;
    }

    /**
     * Set a system property to the given value
     * @param key the key for the system property
     * @param val the value of the system property
     */
    public static void set( String key, String val )
    {
        try {
            // Get the class object
            Class systemProperties = Class.forName("android.os.SystemProperties");

            Method set = systemProperties.getMethod(SET_METHOD, String.class, String.class);

            // Invoke the get method
            set.invoke(null, key, val);

        } catch (Exception e) {
            // We ignore any exception ad return null
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wcsbhwy/article/details/89084961