"Android" Data storage-sharedpreferences Storage

Source: Internet
Author: User

Simple introduction: Sharedpreferences is the use of key-value pairs to store data, that is, when saving a piece of data, to provide a key to the data, if you need to read the data, only through the key to extract the corresponding data.

One: Store data in Sharepreferences

Android offers three main ways to get Sharedpreferences ()

The Getsharedpreferences () method in 1.Context, which receives two parameters, the first parameter is used to specify the file name of the sharedperferences (if the specified file is not created), the second parameter specifies the mode of operation, There are two main modes. Mode_private (by default, as with the 0 effect, it means that only the current program can read and write to this sharedperferences file) and mode_multi_ PROCESS (typically used to read and write to the same sharedpreferences file in multiple threads)

The Getsharedpreferences () in 2.Activity is similar to the context, but only receives one operation mode (using this method will use the current class name as the filename).

The Getdefaultpreferences () method in the 3.PreferenceManager class, which is a static method that receives a context parameter and names the package name of the program as a prefix to the sharedpreferences file.

Once the Sharepreferences object is obtained, the data is stored in three steps to the Sharepreferences file:

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

2. Add data to the Sharepreferences.editor object, what type of data is put on what type, such as putstring () ...

The 3.commit () method submits the added data.

 Public classMainactivityextendsActivity {PrivateButton saveData; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); SaveData=(Button) Findviewbyid (r.id.save_data); Savedata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {sharedpreferences.editor Editor= Getsharedpreferences ("Data", Mode_private). Edit (); Editor.putstring ("Name", "Tom"); Editor.putint ("Age", 28); Editor.putboolean ("Married",false); Editor.commit () ;}});}}

  

Second: reading data from the Sharepreferences

It is easier to read data than to store data, and getsharepreferences () gets the Sharepreferences object first. When storing putstring () on GetString () ...

These corresponding get methods receive two parameters: the key (the key used to pass in the data) and the default value (the default value that will be returned when the incoming key cannot find the corresponding value).

protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); SaveData=(Button) Findviewbyid (r.id.save_data); Restoredata=(Button) Findviewbyid (r.id.restore_data); Restoredata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {sharedpreferences pref= Getsharedpreferences ("Data", mode_private); String name= pref.getstring ("name", "" ");intAge = Pref.getint ("Age", 0);Booleanmarried = Pref.getboolean ("Married",false); LOG.D ("Mainactivity", "name is" +name); LOG.D ("Mainactivity", "Age is" +Age ); LOG.D ("Mainactivity", "Married is" +married);}}); }}

    

"Android" Data storage-sharedpreferences 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.