SharedPreferences for data storage in Android

Source: Internet
Author: User

As a mature mobile phone system, Android provides multiple storage methods for data storage, such as SQLite, SharedPreferences, File, and ContentProvider.,This article mainly introduces SharedPreferences, which is called SP for short. SP is a more lightweight data storage method than databases, it mainly stores simple data required by applications in the form of xml, and the data in xml files is mainly stored in the form of key-value pairs.

SP stores data in a simple way, but it also has defects. Its supported data types include boolean, int, float, long, and String.

The following describes how to use SP to operate data in an application. To use SP, the procedure is as follows:

1. According to the consistent idea of java programming, we first need to obtain the SP object. There are two methods to obtain the SP,

A. getSharedPreferences (String, int). In this way, I can edit the sp data,

B. getPreferences (int mode). From the next purchase, we can see that the main merchant of this method obtains some data of the current activity,

Underlying implementation method of this method

Public SharedPreferences getPreferences (int mode ){
Return getSharedPreferences (getLocalClassName (), mode );
}

Only data can be obtained when an SP object is obtained, but data cannot be operated.

2. Use the Edit () method of SP to obtain the editor object. SP operates on the data through the editor object.

3. Operate data through the editor object. The operation methods are based on the data type. For the fifth data type, the corresponding operation methods are available, such as write: putboolean () and putstring ()GetBoolean () and getString () are read ()

4. Use the Commit () method to submit the data after the data operation is completed.

The data operation process of OK SP is the above four points. Let's take a look at the example below:

 

Public class
Calc extends
Activity {
Public static
Final string prefs_name
= "Myprefsfile ";

@ Override
Protected void oncreate (bundle state ){
Super. oncreate (State );
..
.

// Restore preferences
Sharedpreferences settings
= Getsharedpreferences (prefs_name,
0 );
Boolean silent = settings. getboolean ("silentmode ",
False );
Setsilent (silent );
}

@ Override
Protected void onstop (){
Super. onstop ();

// We need an editor object to make preference changes.
// All objects are from Android. Context. Context
Sharedpreferences settings
= Getsharedpreferences (prefs_name,
0 );
Sharedpreferences. Editor
= Settings. Edit ();
Editor. putboolean ("silentmode", msilentmode );

// Commit the edits!
Editor. commit ();
}
}

If the sp. xml file does not exist after the preceding file is executed, a file is automatically created.

The Directory of this file is generally in/Data/PACKAGE_NAME/Shared_prefs

 

The Application of sp is mainly to save some simple data information, such as the login password, user name or something.

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.