Android sharepreference Shared data synchronization error between main and secondary processes

Source: Internet
Author: User

 

Sharedpreference as one of the most convenient use of Android five storage (network, database, file, Sharedpreference,contentprovider), it is not a tool to store big data from the class name to key/ The value is paired to store the base data type, and only the base data type is stored.

In peacetime development, occasionally encounter sharedpreference in multiple processes to modify the same value occurs. Multi-process refers to the different processes seen in DDMS, including different applications (regardless of whether the share UID is the same), the same application is configured with android:process= "" Multiple processes that are generated. The communication between them will cause the main process to modify the value, which is read by the secondary process when it is initialized the first time.

The solution I have summarized is as follows:

1, the first to get Sharedpreference MODE is set to: Mode_multi_process. Its value is 4 (2.3 has this property later). such as: Getsharedpreferences ("File_test", mode_multi_process);. If this mode is used with Mode_private, then the value written in the other process will not be written to the file.

2, second in order to ensure that the modified data submitted to disk in real-time, do not set the sharedpreference to member variables, as far as possible where to modify the direct access to sharedpreference, modified do not forget the commit.

If the above two points, no accident, the data can be modified in real time.

3. If two processes require timely reading or logical operation based on this value, it is best to use service and AIDL technology between two processes, and aidl is easy for this basic type of delivery.

Finally: For shared data: If you are sharing data instantaneously, you can use the broadcast if you don't need to save it for a long time. Using ContentProvider is the best choice if you want to share a lot of data.

Android sharepreference Shared data synchronization error between main and secondary processes

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.