Android中大量使用属性文件来记录系统的设置和进程间的信息交换; 而且属性文件是全局可见的,每一个进程都可以获取设置属性。

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.

在系统初始化时,Android 就会分配一个共享内存来存储 属性。

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 inshared memory. Any client that wants toGET 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: Native中获取设置属性的方法:

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 “ctl.start” and “ctl.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.

特殊属性:

1, "ro." ----------表示只读属性,该属性值不能被改变;

2, "persist" -------- 一旦该属性被设置,该属性值就会被写入到 /data/property

3.,“net.” ------当属性被设置,"net.change" 会被自动;

4, "ctl.start" / "ctl.stop" ----- 可以用来停止或者启动一个在 /ini.rc中设置了的服务;

例子: 定义bootanim 服务:

service bootanim /system/bin/bootanimation    user graphics    group graphics    disabled    oneshot

关闭开机动画服务:

property_set("ctl.stop", "bootanim");

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 emulatoron 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.

更多相关文章

  1. Android中RelativeLayout及TableLayout使用说明
  2. Android(安卓)service 服务 笔记
  3. android 2.2 apidemos 赏析笔记 4
  4. Android点击左右按钮实现左右滑动页面切换
  5. ImageView ScaleType 属性值的意义
  6. Android网文
  7. Android:shape属性详解(图文并茂)
  8. 类似的Timer循环服务设计
  9. windows 系统Android模拟器联网设置

随机推荐

  1. Android(安卓)网络学习
  2. Android获取View位置getHitRect、getDraw
  3. android工程字符串多语言翻译遗漏查找工
  4. AndroidStudio异常(Plugin "Android Games
  5. 在Android上使用Http客户端的选择(译文)
  6. Android之adt 23找不到NDK路径设置解决方
  7. Windows下Android(安卓)NDK r9 + OpenCV2
  8. Android统计EditText的字母数字以及汉字
  9. android 设置透明度 标题栏 窗体透明
  10. 第八章 Libgdx输入处理(1)