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