Android Local Storage Solution Sharedpreferences

Source: Internet
Author: User



Original address
Storage location

sharedpreferences data saved: /data/data/<package_name>/shared_prefs directory, stored in XML format, the root element is: <map/>. The file name is the parameter value passed when getting the sharedpreferences instance.


<map><int name= "key" value= "value"/><string name= "key" > Value </string></map>


Get instance

shareperferences is an interface, the Context implements the Api:getsharedpreferences (string, int); This API returns a Shareperferences instance based on a string. The same string returns an instance of Shareperferences that is the same. Here strign specifies the name of the XML file where the data is stored.

Activity implements getpreferences (int), which is the default use of 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


   


Write

sharedpreferences The interface itself does not provide the ability to write data, but through its internal interface. It calls the Sharedpreferences.edit () method to get the Sharedpreferences.editor object it corresponds to. The editor has the following methods for writing data to Sharedpreferences:


Empty all the data in the sharedpreferences. Editor.clear ()//to the sharedpreferences to deposit the data corresponding to the specified key. Editor.putxxx (String key, Xxx Value)//delete the data item corresponding to the specified key in Sharedpreferences. Editor.remove (String key)//When editor is finished, call the method to commit the modification. Boolean Editor.commit ()


Read and write sharedpreferences for other applications

1. Create the corresponding 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 app to get the corresponding Sharedpreferences.editor object.



Android Local Storage Solution Sharedpreferences

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.