• Android Property System概述

    Android属性系统
    服务器君一共花费 24.127 ms 进行了 3 次数据库查询,努力地为您提供了这个页面。
    广告很萌的

    Property system is an important feature on android. It runs as a service and manages system configurations and status. All these configurations and status are properties. A property is a key/value pair, both of which are of string type.

    属性(property)系统对Android来说是一个重要的功能。他作为一个系统服务管理着系统的配置和状态,所有的这些系统配置和状态都是属性 (property)。属性(property)是一对键/值(key/value)组合,键和值都是字符串类型。

    From the sense of function, it's very similar to windows registry. Many android applications and libraries directly or indirectly relies on this feature to determine their runtime behavior. For example, adbd process queries property service to check if it's running in emulator. Another example is the java.io.File.pathSeparator returns the value stored in property service.

    总体感觉属性系统非常像Windows的注册表的功能。Android中非常多的应用程序和库直接或者间接的依赖于属性系统,并由此决定其运行期的行为。例如:adbd进程通过属性来决定是否当 前运行在模拟器中。再比如:java.io.File.pathSeparator方法返回存储在属性服务中的值。

    How property system works 属性系统怎样工作

    The high level architecture of property system is shown as following.

    属性系统宏观的结构图如下所示:

    In the figure, there are three processes, a group of persistent property files and a shared memory block. The shared memory block is the container of all property records. Only the property service process can write to the shared memory block. It'll load property records from persistent the save them in the shared memory.

    从图中我们可以看出Android属性系统由有三个进程,一组属性文件和一块共享内存组成。这块共享内存保存着系统中所有的属性记录,只有 Property service能写这块共享内存,并且Property service负责将属性文件中的属性记录加载到共享内存中。

    The consumer process loads the shared memory in its own virtual space and access properties directly. The setter process also loads the shared memory in its virtual space, but it can't write to the memory directly. When the setter tries to add or update a property, it sends the property to property service via unix domain socket. The property service will write the property to shared memory on behalf of the setter process, as well as to the persistent file.

    属性读取进程(property consumer)把这块共享内存映射到自己的进程空间,然后直接读取它。属性设置进程(property setter)也加载这块共享到他的进程空间,但是他不能直接写这块共享内存。当他需要增加或者修改属性的时候,通过Unix Socket发生属性给Property service,Property service将代表设置进程写入共享内存和属性文件。

    Property service runs inside init process. The init process first creates a shared memory region and stores a fd to the region. Then init process maps the region into its virtual space with mmap with MAP_SHARED flag, as a result, any updates to this area can be seen by all processes. This fd and region size are saved in a environment variable named "ANDROID_PROPERTY_WORKSPACE". Any other processes like consumer and setter will use this environment variable to get the fd and size, so that they can mmap this region into its own virtual space. The layout of the shared memory is shown below.

    Property service运行于init进程中。init进程首先创建一块共享内存,并把他的句柄fd存放在这块内存中,init进程通过mmap带 MAP_SHARE标志的系统调用,把这块内存映射到他的虚拟空间中,最终这块内存所有的更新将会被所有映射这块共享内存的进程看到。共享内存句柄fd和 共享内存大小存储在系统环境变量“ANDROID_PROPERTY_WORKSPACE”中,所有的进程包括属性设置进程和属性读取进程都将通过这个系 统环境变量获得共享内存的句柄fd和大小,然后把这块内存映射到他们自己的虚拟空间。共享内存布局如下:

    After that, init process will load properties from following files:

    然后,init进程将会从以下文件中加载属性:

    /default.prop
    /system/build.prop
    /system/default.prop
    /data/local.prop
    

    The next step is start property service. In this step, a unix domain socket server is created. This socket's pathname is "/dev/socket/property_service" which is well known to other client processes.Finally, init process calls poll to wait for connect event on the socket.

    下一步是启动Property service。这步中,将会创建一个Unix Socket服务器,这个Socket有一个闻名的名称“/dev/socket/property_service”。最后init进入死循环,等待socket的连接请求。

    On the consumer side, when it initializes libc(bionic/libc/bionic/libc_common.c __libc_init_common function). It will retrieve the fd and size from environment variable, and map the shared memory into its own space(bionic/libc/bionic/system_properties.c __system_properties_init function). After that, libcutils can read property just as normal memory for the consumer.

    在读取进程中,当它初始化libc库的时候,将会获得属性系统共享内存的句柄和大小(bionic/libc/bionic /libc_init_common.c __libc_init_common函数)。并把这块共享内存映射到自己的进程虚拟空间中(bionic/libc/bionic /system_properties.c __system_properties_init函数)。这样读取进程将会向访问普通内存一样访问属性系统的共享内存了。

    Currently, properties can't be removed. That's to say, once a property has been added, it can't be removed, neither can its key be changed.

    当前,属性不能被删除。也就是说一旦属性被创建,将不可以被删除,但是它们可以被修改。

    How to get/set properties 怎样获得和设置属性

    There are three main means to get/set properies on android.

    在Android中有三种方式来设置和获取属性:

    1. native code

    When writing native applications, property_get and property_set APIs can be used to get/set properties. To use them, we need to include cutils/properties.h and link against libcutils.

    当编写Native的程序时,可以使用property_get和property_set API来获得和设置属性。使用这两个API必须要包含头文件cutils/properties.h和链接libcutil库。

    2. java code

    Android also provides System.getProperty and System.setProperty functions in java library, our java application can use them to get/set properties.

    Android在Java库中提供System.getProperty和System.setProperty方法,我们Java程序可以通过他们来设置和获得属性。

    But it's important to note that although these java APIs are semantically equal to native version, java version store data in a totally different place. Actually, a hashtable is employed by dalvik VM to store properties. So, java properties are separated, it can't get or set native properties, and neither vice versa.

    但是请注意!虽然从语法上面看Java的代码和Native代码非常相近,但是Java版本存储把属性存在其他地方,而不是我们上面提到的属性系统中。在 JVM中有一个hash表来维护Java的属性。所以Java属性和Android属性是不同的,不能用Java API(System.getProperty和System.setProperty)来设置系统属性。也不能通过Native的方法 (property_get和property_set)设置Java的属性。

    Update: Andrew mentioned that android.os.SystemProperties class can manipulate native properties, though it's intended for internal usage only. It calls through jni into native property library to get/set properties.

    更新:Andrew指出android.os.SystemProperties可以操作Android系统属性(虽然这个类倾向于内部使用)。这个类通过JNI调用Native的property_get和property_set方法来获得和设置属性。

    3. shell script Shell脚本

    Android provides getprop and setprop command line tool to retrieve and update properties. They can be used in shell script. They are implemented on top of libcutils.

    Android提供了命令行工具setprop和getprop来设置和获取属性,他们可以在脚本中被使用。

更多 推荐条目

Welcome to NowaMagic Academy!

现代魔法 推荐于 2013-02-27 10:23   

本章最新发布
随机专题
  1. [PHP程序设计] CodeIgniter与PHP框架设计 5 个条目
  2. [软件工程与项目管理] 了解一点WebKit 9 个条目
  3. [移动开发] Android根基概念Context 8 个条目
  4. [Python程序设计] Django Web环境配置 2 个条目
  5. [PHP程序设计] 命令式编程范式 6 个条目
  6. [Linux操作系统] CentOS上使用EPEL Repository 2 个条目
  7. [移动开发] Android Studio里的Gradle 3 个条目
  8. [PHP程序设计] 对输入文件类型的检测 1 个条目
  9. [Python程序设计] Django模板系统 11 个条目
  10. [Python程序设计] Django 入门知识浅介 10 个条目
  11. [搜索引擎优化] 与百度权重有关的信息 2 个条目
  12. [移动开发] Android抽屉导航NavigationDrawer 5 个条目
窗口 -- [博客]