Android系统属性SystemProperties在应用层的用法
2016-11-22 18:04
501 查看
如果你看到这篇文章了,说明你已经是资深程序员,会发现整个Android系统有很多地方有用到这个SystemProperties这个系统属性文件。
关于SystemProperties有什么作用,一般用来嘎哈,大家可以到网上了解它的相关资料,还是比较多的。
最常用的方法是SystemProperties.set()/SystemProperties.get(),通过这两个接口可以对系统的属性进行设置/读取。
但是,这两个接口并没有对应用层开放。目前,有两种方法,可以实现应用层使用这两个接口:
1,项目导入系统jar包,这样就可以直接导入相应的包名用了。
导入系统jar包的方法参考:http://blog.csdn.net/lilidejing/article/details/53269896
2,利用java反射
代码如下,示例分别是调用get和set:
/** * Get the value for the given key. * @return an empty string if the key isn't found * @throws IllegalArgumentException if the key exceeds 32 characters */ public static String get(String key) { String result=""; try { Class<?> c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class); result=(String)get.invoke(c, key); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } /** * Set the value for the given key. * @throws IllegalArgumentException if the key exceeds 32 characters * @throws IllegalArgumentException if the value exceeds 92 characters */ public static void set(String key, String val) { try { Class<?> c = Class.forName("android.os.SystemProperties"); Method set = c.getMethod("set", String.class,String.class); set.invoke(c, key,val); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
应用要使用SystemProperties的属性,前提是应用需要有系统权限,即要系统签名。系统签名的方法可以参考:http://blog.csdn.net/lilidejing/article/details/45098879
相关文章推荐
- Android系统属性SystemProperties在应用层的用法【转】
- android Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结(转)
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结(转)
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android SystemProperties设置/取得系统属性的用法总结
- Android 的系统属性(SystemProperties)设置分析
- Android 的系统属性(SystemProperties)分析