Android Local Storage Solution Sharedpreferences

Source: Internet
Author: User



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

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.