android 反射用法实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuxingchong/article/details/81020572

public static String getSystemProperty(String property, String defaultValue) {
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method getter = clazz.getDeclaredMethod("get", String.class);//方法名,参数类型
            String value = (String) getter.invoke(clazz.newInstance(), property);
            if (!TextUtils.isEmpty(value)) {
                return value;
            }
        } catch (Exception e) {
            QLog.d(TAG, "Unable to read system properties");
        }
        return defaultValue;
    }
public static class WallpaperManagerApis {
        public static Drawable getLockScreenDrawable(WallpaperManager mWallpaperManager) {
            try {
                Class clss = Class.forName("android.app.WallpaperManager");
                Method method = clss.getMethod("getLockScreenDrawable", null);//方法名,参数个数,无参数
                method.setAccessible(true);
                try {
                    Drawable rtn = (Drawable) method.invoke(mWallpaperManager);//调用方法,没有参数
                    return rtn;
                } catch (IllegalAccessException e) {
                    QLog.d(TAG,"getLockScreenDrawable can't getLockScreenDrawable");
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }


猜你喜欢

转载自blog.csdn.net/zhuxingchong/article/details/81020572