Original address: http://www.yanwushu.com/post/43.html
Storage location
sharedpreferences data saved: /data/data/<package_name>/shared_prefs folder, save in XML format, the root element is: <map/>. The file name is the number of parameters passed when getting the sharedpreferences instance.
<map><int name= "key" value= "value"/><string name= "key" > Value </string></map>
Get instance
shareperferences is an interface that implements the Api:getsharedpreferences (string, int); This API returns a Shareperferences instance based on a string. The same string returns a Shareperferences instance of the same. Here strign specifies the name of the XML file where the data is stored.
Activity getpreferences (int) is implementedby default using the activity's class name as a string called getsharedpreferences (string, int).
Specifies that the sharedpreferences data can only be read and written by the application.Context.mode_private//Specifies that the Sharedpreferences data can also be read by other applications, but cannot be written. Context.mode_world_readable//Specifies that the Sharedpreferences data can also be read and written by other applications. The context.mode_world_writeable//file exists, so append is present, or new context.mode_world_append
Read
Infer whether sharedpreferences includes data for a specific key. Boolean contains (String key)//gets all the Key-value pairs in the sharedpreferences. Map<string,?> getAll ()//Gets the value corresponding to the specified key, assuming that key does not exist. The default value of Defvalue is returned.
Write
sharedpreferences The interface itself does not provide the ability to write data, but through its internal interface. Its invocation of the Sharedpreferences.edit () method allows it to obtain the corresponding Sharedpreferences.editor object. Editor has the following methods for writing data to Sharedpreferences:
Empty all the data in the sharedpreferences. Editor.clear ()//sharedpreferences the corresponding data to the specified key.Editor.putxxx (String key, Xxx Value)//delete the corresponding data item of the specified key in Sharedpreferences.
Editor.remove (String key)//When editor is finished, call the method to commit the changes. Boolean Editor.commit ()
Read and write sharedpreferences for other applications
1. Create the appropriate Context for the other app.
Context usecontext =createpackagecontext ("Package_name", context.context_ignore_security);
2. Call the Getsharedpreferences () method of the Context of the other app to get the Sharedpreferences object.
3. Call the Sharedpreferences.edit () method of the other application to obtain the corresponding Sharedpreferences.editor object.
Android Local Storage Solution Sharedpreferences