http://blog.csdn.net/jerryutscn/article/details/5519423

Android的属性系统

每条属性包含了名字和其对应的值,两者都用字符串来描述。Android系统中大量的使用了属性系统用于记录系统的设置(注:和windows系统里的注册表类似),以及进程间的信息交互。属性系统对于整个系统来说是全局的,也就是说每一个进程都可以获取和设置每条属性。

当系统初始化的时候,Android系统会分配一块共享内存用于存储属性信息。这些操作是在"init"这个守护进程里完成的,其对应的源代码目录在:device/system/init。"init"守护进程同时也启动了属性服务。属性服务运行在init进程中(注:是线程?)。客户端需要设置属性时,需要连接到属性服务上,同时发送消息给属性服务。这样属性服务就可以更新保存在共享内存中的属性信息。当客户端需要读取属性信息时,可以直接访问共享内存,这样做可以提高性能。

客户端程序可以通过调用libcutils库里的API函数实现对属性的设置和读取的操作。

libcutils库对应的源代码存放在: device/libs/cutils。

这些API函数包括:

int property_get(const char *key, char *value, const char *default_value);

int property_set(const char *key, const char *value);

    libcutils库是通过调用libc库中的__system_property_xxx这一系列函数实现从共享内存中获取属性的。对应的libc的源代码保存在: device/system/bionic。

属性服务也会调用libc库中的函数__system_property_init来实现对共享内存的初始化。当启动属性服务时会通过下面文件来加载默认的属性信息。

/default.prop

/system/build.prop

/system/default.prop

/data/local.prop

属性信息按照上面的顺序被加载。后加载的属性会覆盖前面的属性值(注:当属性名称相同的时候)。当上面加载完成后,最后加载的是驻留属性,保存在/data/property文件中。

特别的属性

如果属性是有”ro.”字符串开头,那么这条属性被看成一个只读属性,一旦设置就不能被修改了。(注:初始化设置的时候是可写的)

如果属性是有“persist.”字符串开头,那么就认为是驻留属性,当修改的时候同时也会被保存在/data/property文件中。

如果属性是有“net.”字符串开头,当设置这种属性的时候,“net.change”这条属性也会被自动设置,其内容设为最后更新过的属性名。

属性“ctrl.start” 和 “ctrl.stop” 用于启动和停止服务。这些服务必须在文件/init.rc中被定义。在系统启动的时候,init守护进程会解析init.rc文件,然后启动属性服务。一旦收到了“ctrl.start”这个属性的设置请求,属性服务会根据该属性的值作为服务名,在列表中找到服务对象并启动。服务启动的结果会反映在“init.svc.<service name>”这个属性之中。客户端程序可以轮询该属性值来判断服务的启动结果。

Android 工具箱

Android 工具箱提供了2个应用程序:setprop 和 getprop用于读取和设置属性项目。使用方法是:

getprop <property name>

setprop <property name> <property value>

Java

Java程序可以通过使用函数System.getProperty() 和 System.setProperty()获取和设置属性条目。

动作

默认情况下设置属性只会触发init进程对共享内存进行操作,并不会执行任何脚本和程序。但是你可以修改文件init.rc,这样在属性值发生变化的时候来触发你定义的动作。例如在默认的init.rc文件中你可以找到下面这部分:

# adbd on at boot in emulator

on property:ro.kernel.qemu=1

    start adbd

on property:persist.service.adb.enable=1

    start adbd

on property:persist.service.adb.enable=0

    stop adbd

所以当属性条目persist.service.adb.enable被设置成1的时候,init守护进程知道他需要启动adbd服务。

原文如下:

Android Property System

Every property has a name and value. Both name and value are text strings. Property is heavily used in Android to record system setting or exchange information between processes. The property is globally visible in the whole system. Every process can get/set a property.

On system initialization, Android will allocates a block of shared memory for storing the properties. This is done in “init” daemon whose source code is at: device/system/init. The “init” daemon will start a Property Service. The Property Service is running in the process of “init” daemon. Every client that wants to SET property needs to connect to the Property Service and send message to Property Service. Property Service will update/create the property in shared memory. Any client that wants to GET property can read the property from the shared memory directly. This promotes the read performance.

The client application can invoke the API function exposed from libcutils to GET/SET a property.

The source code of libcutils locates at: device/libs/cutils.

The API function is:

int property_get(const char *key, char *value, const char *default_value);

int property_set(const char *key, const char *value);

The libcutils is in turn calling the __system_property_xxx function in libc to get a property from the shared memory. The source code of libc is at: device/system/bionic.

The Property Service is also in turn calling the __system_property_init function in libc to initiate the shared memory for properties. When starting the Property Service will load the default properties from below files:

/default.prop

/system/build.prop

/system/default.prop

/data/local.prop

The properties are loaded in the above order. Later loaded properties will override the previous values. After those properties are loaded, the last loaded is the persistent properties which is persisted in /data/property.

Special Properties

If a property’s name begins with “ro.”, then this property is treated as a read-only property. Once set, the value of the property can’t be changed.

If a property’s name begins with “persist.”, then when setting this property, the value will be written to /data/property, too.

If a property’s name begins with “net.”, when when setting this property, the “net.change” property will be set automatically to contain the name of the last updated property. (It’s tricky. The netresolve module uses this property to track if there is any change on the net.* properties.)

The property “ctrl.start” and “ctrl.stop” is used to start and stop a service. Every service must be defined in /init.rc. On system startup, the init daemon will parse the init.rc and start the Property Service. Once received a request to set the property of “ctrl.start”, the Property Service will use the property value as the service name to find the service and then start the service. The service starting result is then put to the property “init.svc.<service name>”. The client application can poll the value of that property to determine the result.

Android toolbox

The Android toolbox provides two applets: setprop and getprop to get and set properties. The usage is:

getprop <property name>

setprop <property name> <property value>

Java

The java application can use the System.getProperty() and System.setProperty() function to Get and Set the property.

Action

By default the set property will only cause "init" daemon to write to shared memory, it won't execute any script or binary. But you can add your actions to correspond to property change in init.rc. For example, in the default init.rc, you can find.

# adbd on at boot in emulator

on property:ro.kernel.qemu=1

    start adbd

on property:persist.service.adb.enable=1

    start adbd

on property:persist.service.adb.enable=0

    stop adbd

So if you set persist.service.adb.enable to 1, the "init" daemon knows it has actions to do, then it will start adbd service.

Android的属性系统的更多相关文章

  1. Android属性系统简介

    1.简介 在android 系统中,为统一管理系统的属性,设计了一个统一的属性系统.每个属性都有一个名称和值,他们都是字符串格式.属性被大量使用在Android系统中,用来记录系统设置或进程之间的信息 ...

  2. Android 属性系统 Property service 设定分析 (转载)

    转自:http://blog.csdn.net/andyhuabing/article/details/7381879 Android 属性系统 Property service 设定分析 在Wind ...

  3. Android属性系统简介【转】

    本文转载自:http://www.cnblogs.com/l2rf/p/6610348.html 1.简介 在android 系统中,为统一管理系统的属性,设计了一个统一的属性系统.每个属性都有一个名 ...

  4. android:exported 属性详解

    属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...

  5. 让Android程序获得系统的权限,实现关机重启,静默安装等功能

    引用:http://www.cnblogs.com/welenwho/archive/2012/05/10/2494984.html android想要获得系统权限有几种途径,一种就是你的程序固化的系 ...

  6. Android:关于声明文件中android:process属性说明

    笔者在学习Android Service组件的过程中碰到了一个问题,就是在Android应用的声明文件Manifest.xml中有时候会对相关的服务标签设置一个android:process=&quo ...

  7. android service被系统回收的解决方法

    自己的app的service总是容易被系统回收,搜罗了一下,基本上的解决思路有以下几种: 1.把service写成系统服务,将永远不会被回收(未实践): 在Manifest.xml文件中设置persi ...

  8. Android应用与系统安全防御

    来源:HTTP://WWW.CNBLOGS.COM/GOODHACKER/P/3864680.HTML ANDROID应用安全防御 Android应用的安全隐患包括三个方面:代码安全.数据安全和组件安 ...

  9. android:sharedUserId 获取系统权限

    最近在做的项目,有好大一部分都用到这个权限,修改系统时间啊,调用隐藏方法啊,系统关机重启啊,静默安装升级卸载应用等等,刚开始的时候,直接添加权限,运行就报错,无论模拟器还是真机,在logcat中总会得 ...

随机推荐

  1. OpenGL学习——基本概念和坐标变换

    基本概念 基本功能:几何图形.变换.着色.光照.贴图 高级功能:曲面图元.光栅操作.景深.shader编程   状态机 先设置状态参数:多边形.顶点列表.填充颜色.纹理.混合模式.坐标系 再调用绘图指 ...

  2. UML统一建模语言

    概述 统一建模语言(UML)是一种图形化的语言,用于软件密集系统要素的可视化.制定规范.构建对象和编写文档.UML提供了一种标准的方式来描述系统的设计图,既包括概念方面,例如业务过程和系统功能,也包括 ...

  3. Chapter3:字符串、向量和数组

    C++11新特性:范围for(range for) for (declaration: expression) statement vector和数组都是对象的集合,而引用不是对象. vector对象 ...

  4. C# 引用类型的"祸害"

    前端时间刚刚弄完一个项目,终于有时间来总结与回顾了. 项目需求:给用户发送邮件,邮件分为系统邮件和个人邮件,需要按时间.未读降序排列. 一开始以为,这是一个很简单的需求,给邮件建了一个对象: clas ...

  5. Codevs No.1287 矩阵乘法

    2016-06-01 16:53:23 题目链接: 矩阵乘法 (Codevs No.1287) 题目大意: 给你两个可乘矩阵a,b,求a*b 解法: 定义....... //矩阵乘法 (Codevs ...

  6. hibernate hbm2ddl auto 不能创建表的问题

    http://fuckgis.blog.sohu.com/148751122.html ________________________________________________________ ...

  7. JavaSE聊天室

    今天学习了一下简单聊天程序(类似QQ那种)的编写过程,从最初的0.1版本到最后的1.3版本,功能不断地增强,下面对一天的学习内容进行梳理. 版本0.1 我们的需求是显示一个窗体,其他什么也不用做,其他 ...

  8. memcached全面剖析–4. memcached的分布式算法

    memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第3次 前坂介绍的内存存储功能,其实现 ...

  9. URAL 2065 Different Sums (找规律)

    题意:构造一个数列,使得它们的区间和的种类最少,其中数列中不同的数的数目不少于k. 析:我们考虑0这个特殊的数字,然后0越多,那么总和种类最少,再就是正负交替,那么增加0的数量. 代码如下: #pra ...

  10. poj 3635 Full Tank? ( bfs+dp思想 )

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5857   Accepted: 1920 Descri ...