概述
Android的系统属性相当于Windows的注册表,由key和value组成,且都是核心系统的一个基本机制。相对于Windows的注册表,Android的系统属性要简单一些,它没有Windows注册表的树状结构,而只是一个列表,也就是说没有父子关系。value有string,int,long,boolean,但是设置只能通过字符串方式。
读取系统属性,是通过SystemProperties类来实现的。SystemProperties在android.os下,但这个类是隐藏的,上层程序开发无法直接使用。要使用这个类,有两种方法。一个是导入layoutlib.jar,另外一种是通过反射的方式调用。
导入layoutlib.jar
我们只介绍一下如何在Android Studio中导入的方式
在gradle配置文件中,写一个函数,动态获取layoutlib.jar路径,然后加到dependencies中即可,代码如下:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  | 
						dependencies {     provided files(getLayoutLibPath()) } /** ZhangChao time:2014-12-31,get layoutlib.jar path. android.os.SystemProperties need it. */ // must called after "android" definition def getLayoutLibPath() {     def rootDir = project.rootDir     def localProperties = new File(rootDir, "local.properties")     if (localProperties.exists()) {         Properties properties = new Properties()         localProperties.withInputStream {             instr -> properties.load(instr)         }         def sdkDir = properties.getProperty('sdk.dir')         def compileSdkVersion = android.compileSdkVersion         Console.println("app compileSdkVersion : " + compileSdkVersion)         def androidJarPath = sdkDir + "/platforms/" + compileSdkVersion + "/data/layoutlib.jar"         return androidJarPath     }     return rootDir }  | 
					
导入之后,直接
| 
					 1  | 
						import android.os.SystemProperties;  | 
					
就可以正常使用了。
注意:引入的layoutlib.jar并不会编译到APK包里面,因此不需要担心增加最终的APK的大小的情况。
反射调用
对于不想引入layoutlib.jar的情况,可以直接使用下面的反射类来实现调用。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  | 
						import java.lang.reflect.Method; public class PropertyUtils {     private static volatile Method set = null;     private static volatile Method get = null;     public static void set(String prop, String value) {         try {             if (null == set) {                 synchronized (PropertyUtils.class) {                     if (null == set) {                         Class<?> cls = Class.forName("android.os.SystemProperties");                         set = cls.getDeclaredMethod("set", new Class<?>[]{String.class, String.class});                     }                 }             }             set.invoke(null, new Object[]{prop, value});         } catch (Throwable e) {             e.printStackTrace();         }     }     public static String get(String prop, String defaultvalue) {         String value = defaultvalue;         try {             if (null == get) {                 synchronized (PropertyUtils.class) {                     if (null == get) {                         Class<?> cls = Class.forName("android.os.SystemProperties");                         get = cls.getDeclaredMethod("get", new Class<?>[]{String.class, String.class});                     }                 }             }             value = (String) (get.invoke(null, new Object[]{prop, defaultvalue}));         } catch (Throwable e) {             e.printStackTrace();         }         return value;     } }  | 
					








