Android Property System

Source: Internet
Author: User

 
The property system is an important feature for Android. As a system service, it manages system configurations and statuses. All these system configurations and statuses are attributes.
(Property ). Property is a key/value combination, and the key and value are both string types. In general, the property system is very similar to Windows.
. Many applications and libraries in androd depend directly or indirectly on the attribute system, and thus determine the behavior during the runtime. For example, the adbd process determines whether to use the attribute
Run in the simulator. For example, the Java. Io. file. pathseparator method returns the value stored in the property service.

How does the property system work?

The macro structure of the property system is as follows:


We can see that the android property system consists of three processes, a group of Property Files and a shared memory. This shared memory stores all the attribute records in the system, only
Property Service can write this shared memory, and property service is responsible for loading the property records in the property file into the shared memory.

Property read process (Property
Consumer) maps this shared memory to its own process space and then directly reads it. Property setting process (Property
Setter) also loads this shared to his process space, but he cannot directly write this shared memory. When he needs to add or modify attributes
When a socket property is sent to the property service, the property service sets the process to write data to the shared memory and property files.

Property
The service is running in the INIT process. The INIT process first creates a shared memory and stores its handle FD in this memory. The INIT process uses the MMAP
The system call of the map_share flag maps the memory to its virtual space. In the end, all updates to the memory will be seen by all processes mapped to the shared memory. Shared Memory handle FD and
The shared memory size is stored in the system environment variable "android_property_workspace". All processes, including attribute setting processes and attribute reading processes, use this system
Environment Variables get the FD and size of the shared memory handle, and then map the memory to their own virtual space. The shared memory layout is as follows:

The INIT process then loads the attributes from the following files:

   1: /default.prop
   2: /system/build.prop
   3: /system/default.prop
   4: /data/local.prop

The next step is to start property service. In this step, a UNIX socket server will be created, which has a well-known name "/dev/socket/property_service ". Finally, init enters an endless loop and waits for the socket connection request.


In the read process, when it initializes the libc library, it will obtain the handle and size of the property system shared memory (bionic/libc/bionic
/Libc_init_common.c
_ Libc_init_common function ). And map the shared memory to the virtual space of the process (bionic/libc/bionic ).
/System_properties.c
_ System_properties_init function ). In this way, the read process will access the shared memory of the attribute system just like accessing normal memory.

Currently, attributes cannot be deleted. That is to say, once a property is created, it cannot be deleted, but they can be modified.

How to obtain and set attributes

There are three ways to set and obtain attributes in Android:

1. native code

When writing native programs, you can use the property_get and property_set APIs to obtain and set properties. To use these two APIs, you must include the header file cutils/properties. h and the libcutil library.

2. Java code

Android provides the system. getproperty and system. setproperty methods in the Java library. Our Java program can use them to set and obtain properties.


But please note! Although Java code and native code are very similar in terms of syntax, Java version storage stores attributes elsewhere, rather than in the property system we mentioned above. In
The JVM has a hash table to maintain Java attributes. Therefore, Java and Android attributes are different.
API (system. getproperty and system. setproperty) to set system properties. Or the native method.
(Property_get and property_set) set Java attributes.

Update:AndrewIt is pointed out that android. OS. systemproperties can be used to operate Android system properties (although this class tends to be used internally ). This class uses JNI to call native's property_get and property_set methods to obtain and set attributes.

3. shell script

Android provides the command line tools setprop and getprop to set and obtain attributes. They can be used in scripts.

Original article: http://rxwen.blogspot.com/2010/01/android-property-system.html


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.