Sharedpreferences storage of Android data storage

Source: Internet
Author: User

------------------------------------------sharedpreferences Storage ----------------------------------------- ---

Sharedpreferences uses key-value pairs to store data. when you save a piece of data, you need to give the data a corresponding key, so that when you read the data, you can use this key to remove the corresponding value.

Sharedpreferences supports storage of many different data types, and can be used to remove numeric or character-based data as needed.

----------------------------------------- Gets the Sharedpreferences object ---------------------------------------- -

To use Sharedpreferences to store data, you first get to the Sharedpreferences object. Android mainly provides three ways to get sharedpreferences objects.

1. The Getsharedpreferences () method in the Context class

This method accepts two parameters, the first parameter specifies the name of the sharedpreferences file , and one is created if the specified file does not exist.

sharedpreferences files are stored in the /data/data/< package name >/shared_prefs/directory .

The second parameter specifies the mode of operation , mainly mode_private (the default mode of operation) and mode_multi_process (multi-process read-write).

2. The Getpreferences () method in the Activity class

This method is similar to the Getsharedpreferences () method in the Context, but it only receives an operation mode parameter, which automatically uses the currently active class name as the sharedpreferences filename.

3. Getdefaultsharedpreferences () method in the Preferencemanager class

This is a static method that receives a Context parameter and automatically uses the current application's package name as a prefix to name the sharedpreferences file.

Once you have the Sharedpreferences object, you can begin to store the data in the Sharedpreferences file.

--------------------------------------- Store the data in Sharepreferences --------------------------------------

To store data using the Sharedpreferences object:

1. Call the edit () method of the Sharedpreferences object to get a Sharepreferences.editor object.

2. Add data to the Sharedpreferences.editor object, use the putstring () method If you add a string, and so on.

3. The commit () method is called to commit the data that was added, thus completing the data store operation.

To store the data in a sharedpreferences file:

             Public voidOnClick (View v) {//Get Sharedpreferences ObjectSharedpreferences shared = getsharedpreferences ("Data", mode_private); //Get Sharedpreferences.editor ObjectSharedpreferences.editor Editor =Shared.edit (); //incoming data as key-value pairsEditor.putstring ("name", "Jay Chou"); Editor.putint ("Age", 37); Editor.putboolean ("Married",true); //Submit DataEditor.commit (); }

The resulting Data.xml file content:

<?XML version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><Map>    <stringname= "Name">Jay Chou</string>    <intname= "Age"value= "Panax Notoginseng" />    <Booleanname= "Married"value= "true" /></Map>

You can see that all the data has been saved successfully, and the Sharedpreferences file is managed using XML format.

Note: If you want to clear all the data in the XML , you can clear the data in the Sharedpreferences file by using the Clear () method of the Sharedpreferences.editor object .

--------------------------------------- read data from the sharepreferences --------------------------------------

Reading a file is easier than storing it from a sharedpreferences file. The Sharedpreferences object provides a series of get methods for reading stored data, each of which corresponds to a put method in Sharedpreferences.editor.

These get methods receive two parameters, the first parameter is the key , the key that is used to pass in the stored data can get the corresponding value, the second parameter is the default value , that is, when the incoming key cannot find the corresponding value, What default values will be returned.

Get the Sharedpreferences file data and print it to the console:

             Public voidOnClick (View v) {//Get Sharedpreferences ObjectSharedpreferences shared = getsharedpreferences ("Data", mode_private); //Get XML DataString name = shared.getstring ("name", "null"); intAge = Shared.getint ("Age", 0); Booleanmarried = Shared.getboolean ("Married",false); //get data that does not existString hobby = shared.getstring ("hobby", "Unknown"); //Print log recordsLOG.D ("Woider", "Name:" +name); LOG.D ("Woider", "Age:" +Age ); LOG.D ("Woider", "Married:" +married); LOG.D ("Woider", "Hobby:" +hobby); }

Log:

Sharedpreferences storage of Android data storage

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.