Android 获取系统prop

来源:互联网 发布:2016党规知识网络测试 编辑:程序博客网 时间:2024/05/23 12:16

Android 系统prop java层的api 定义在类 android.os.SystemProperties 中
由于这个类是隐藏的,所以我们需要采用反射的方法去调用

反射调用

private String getProp(String key,String defaultValue){        String value = defaultValue;        try {            Class<?> c = Class.forName(SYSPropClass);            Method get = c.getMethod("get",String.class,String.class);            value = (String) get.invoke(c,key,"unknow");        } catch (ClassNotFoundException e) {            Log.d("MainActivity", e.getMessage());        } catch (NoSuchMethodException e) {            Log.d("MainActivity", e.getMessage());        } catch (InvocationTargetException e) {            Log.d("MainActivity", e.getMessage());        } catch (IllegalAccessException e) {            Log.d("MainActivity", e.getMessage());        }        return  value;    }调用方法:    String value = getProp("ro.product.model","")
0 0
原创粉丝点击