Android的属性系统

时间:2022-04-19 10:25:55
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&colon;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&colon;关于声明文件中android&colon;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&colon;sharedUserId 获取系统权限

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

随机推荐

  1. HDU 1859

    #include <iostream> #include <cstdio> #include <algorithm> #include <vector> ...

  2. 原生js发送ajax请求

    堕落了一阵子了,今天打开博客,发现连登录的用户名和密码都不记得了.2016年已过半,不能再这么晃荡下去了. 参加了网易微专业-前端攻城狮 培训,目前进行到大作业开发阶段,感觉举步维艰.但是无论如何,不 ...

  3. 网络数据包收发流程&lpar;四&rpar;:协议栈之packet&lowbar;type

    进入函数netif_receive_skb()后,skb正式开始协议栈之旅.先上图,协议栈大致过程如下所示:跟OSI七层模型不同,linux根据包结构对网络进行分层.比如,arp头和ip头都是紧跟在以 ...

  4. Linux启动&sol;停止&sol;重启Mysql数据库的方法——转载

    Mysql启动.停止.重启常用命令 a.启动方式1.使用 service 启动:[root@localhost /]# service mysqld start (5.0版本是mysqld)[root ...

  5. node&period;js中log4js的使用

    以前用过forever进程守护的日志记录到指定文件,但是只能保存到一个文件中不能分片,这样到只日志文件越来越大, forever start -s -l ./forever.log app.js -l ...

  6. 解决tomcat was unable to start within问题

    这个问题可能大家都熟悉,以前碰到这个问题,重新启动一次eclipse就好了,随着我的一个项目的增大,我发现这种情况越来越多,到底是怎么回事? 出现这个情况的原因有两个,要么是你的数据库连接connec ...

  7. Array原型链添加&OpenCurlyDoubleQuote;遍历”方法

    <script> //1.在我们之前的项目里向原型链中集成方法时大多代码分析不严密,有时间我在这里会做详细分析; Array.prototype.each = function(fn) { ...

  8. Destoon QQ互联一键登录审核不通过的解决方案

    在QQ互联上申请帐号之后提交了审核, 后台填写APPID和KEY之后自己申请的QQ号可以正常登录,但QQ互联审核的时候一直审核不通过说是“您的网站审核未通过,原因是“点击QQ登录按钮提示登录失败或出现 ...

  9. oracle帐号scott被锁定如何解锁

       具体操作步骤如下:  C:> sqlplus  请输入用户名:sys  输入口令:sys as sysdba //注意:在口令这里输入 的密码后面必须要跟上 as sysdba 才可以.  ...

  10. i春秋------Misc更新

    今天早上起来很开森!因为今天要打比赛了(2018年3月安恒杯线上赛),等到比赛开始得时候,发现自己登陆不上去 想了很久发现自己只是预约了比赛,并没有报名(QAQ ),心疼一下傻傻的自己.现在开始工作: ...