Android的属性系统
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的属性系统的更多相关文章
- Android属性系统简介
1.简介 在android 系统中,为统一管理系统的属性,设计了一个统一的属性系统.每个属性都有一个名称和值,他们都是字符串格式.属性被大量使用在Android系统中,用来记录系统设置或进程之间的信息 ...
- Android 属性系统 Property service 设定分析 (转载)
转自:http://blog.csdn.net/andyhuabing/article/details/7381879 Android 属性系统 Property service 设定分析 在Wind ...
- Android属性系统简介【转】
本文转载自:http://www.cnblogs.com/l2rf/p/6610348.html 1.简介 在android 系统中,为统一管理系统的属性,设计了一个统一的属性系统.每个属性都有一个名 ...
- android:exported 属性详解
属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...
- 让Android程序获得系统的权限,实现关机重启,静默安装等功能
引用:http://www.cnblogs.com/welenwho/archive/2012/05/10/2494984.html android想要获得系统权限有几种途径,一种就是你的程序固化的系 ...
- Android:关于声明文件中android:process属性说明
笔者在学习Android Service组件的过程中碰到了一个问题,就是在Android应用的声明文件Manifest.xml中有时候会对相关的服务标签设置一个android:process=&quo ...
- android service被系统回收的解决方法
自己的app的service总是容易被系统回收,搜罗了一下,基本上的解决思路有以下几种: 1.把service写成系统服务,将永远不会被回收(未实践): 在Manifest.xml文件中设置persi ...
- Android应用与系统安全防御
来源:HTTP://WWW.CNBLOGS.COM/GOODHACKER/P/3864680.HTML ANDROID应用安全防御 Android应用的安全隐患包括三个方面:代码安全.数据安全和组件安 ...
- android:sharedUserId 获取系统权限
最近在做的项目,有好大一部分都用到这个权限,修改系统时间啊,调用隐藏方法啊,系统关机重启啊,静默安装升级卸载应用等等,刚开始的时候,直接添加权限,运行就报错,无论模拟器还是真机,在logcat中总会得 ...
随机推荐
- Linux性能监控之Memory篇
首先说说虚拟内存和物理内存: 虚拟内存就是采用硬盘来对物理内存进行扩展,将暂时不用的内存页写到硬盘上而腾出更多的物理内存让有需要的进程来用.当这些内存页需要用的时候在从硬盘读回内存.这一切对于用户来说 ...
- Django settings — Django 1.6 documentation
Django settings - Django 1.6 documentation export DJANGO_SETTINGS_MODULE=mysite.settings django-admi ...
- 【转】Maven实战(五)---两个war包的调用
原博文出自于: http://blog.csdn.net/liutengteng130/article/details/42879803 感谢! 开篇前提 1.为什么要用两个war包的调用? ...
- Java邮件服务学习之一:邮件服务概述
java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,另外就是使用java编写邮件服务端:两者区别在于客户端只负责给终端客户收发邮件,就相当于小区楼下的那一排排的铁皮邮箱盒,而邮 ...
- PLSQL Developer 常用设置及快捷键
1.登录后自动选中My Objects(已验证可用) 默认情况下,PLSQL Developer登录后,Brower里会选择all Objects,如果你登录的用户是DBA, 要展开tables目录, ...
- UVa 1252 Twenty Questions (状压DP+记忆化搜索)
题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...
- java选项及系统属性
java选项 -d32 使用 32 位数据模型 (如果可用) -d64 使用 64 位数据模型 (如果可用) -server 选择 "server" VM 默认 VM 是 serv ...
- 【Java】Treeset实现自定义排序
两个类,一个学生类,含姓名和出生日期两个属性:还有一个学生排序类,重写compare函数,自定义排序规则是先比较出生日期,如果相同再比较姓名字母 package birthday; import ja ...
- 用Rufus来制作Windows10的U盘安装盘
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用Rufus来制作Windows10的U盘安装盘.
- 项目经验之:GIS的初步工作窗体的搭建
不多说了,上图,初步工作刚好完 GIS平台系统,实现整个供水系统的协调与统一.系统以管网为基础依据,建立可实现供水管网规划设计.输配管理.图档管理.抢修辅助决策及综合查询.统计等功能. 整体窗口设计 ...