Android Property System

Source: Internet
Author: User
1. What is an attribute?

The property system is an important feature for Android. As a system service, it manages system configurations and States. All these system configurations and states are properties ). Property is a key/value combination, and the key and value are both string types. In general, the property system is very similar to the Windows registry function. 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 or not it is currently running in the simulator through attributes. For example, the Java. Io. file. pathseparator method returns the value stored in the property service.

2. 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 property records in the system. Only the property service can write this shared memory, and the property service is responsible for loading the property records in the property file into the shared memory.

Property consumer maps the shared memory to its own process space and then directly reads it. Property setter also loads the shared process space, but he cannot directly write the shared memory. When an attribute needs to be added or modified, property service is assigned to the property service through UNIX socket. Property Service indicates that the process is set to write data to shared memory and property files.

Property Service runs in the INIT process. The INIT process first creates a shared memory and stores its handle FD in this memory. The INIT process is called by the MMAP system with the map_share flag, map this memory to its virtual space. In the end, all updates to this memory will be seen by all processes mapped to this shared memory. The shared memory handle FD and shared memory size are stored in the system environment variable "android_property_workspace, all processes, including attribute setting processes and attribute reading processes, will obtain the FD and size of the shared memory handle through this system environment variable, and then map this 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.

3. 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. There is a hash table in JVM to maintain Java attributes. Therefore, the Java and Android attributes are different. You cannot use Java APIs (system. getproperty and system. setproperty) to set system attributes. You cannot use the native method (property_get and property_set) to set Java attributes.

Update: Andrew points out that android. OS. systemproperties can be used to operate on 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.

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.