概述
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中即可,代码如下:
Android Studio Import layoutlib.jar
Vim
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.withInput