Overview
SharedPreferences is easy to use and can easily store and read data. SharedPreferences can only store simple types of data, such as String and int. Generally, the data of complex types is converted to Base64 encoding, And the converted data is saved as a string in the XML file, and then saved in SharedPreferences.
To save the key-value Pair using SharedPreferences, follow these steps:
(1) Use the getSharedPreferences method of the Activity class to obtain the SharedPreferences object. The name of the file storing the key-value is specified by the first parameter of the getSharedPreferences method.
(2) Use the edit interface of the SharedPreferences interface to obtain the SharedPreferences. Editor object.
(3) Use the putXxx method of the SharedPreferences. Editor interface to save the key-value pair. Xxx indicates different data types. For example, the putString method is required for string-type values.
(4) use the commit method of the SharedPreferences. Editor interface to save the key-value pair. The commit method is equivalent to a commit operation in a database transaction.
Procedure
The code writing process is as follows:
A. Store Data Information
// 1. Open Preferences with the name setting. If yes, open Preferences. Otherwise, create a new Preferences.
SharedPreferences settings = getSharedPreferences ("setting", 0 );
// 2. Set setting to edit
SharedPreferences. Editor editor = settings. edit ();
// 3. Store Data
Editor. putString ("name", "ATAAW ");
Editor. putString ("URL", "ATAAW. COM ");
// 4. Submit
Editor. commit ();
B. Read data information
// 1. Get Preferences
SharedPreferences settings = getSharedPreferences ("setting", 0 );
// 2. Retrieve Data
String name = settings. getString ("name", "Default Value ");
String url = setting. getString ("URL", "default ");
// The above is how to use SharedPreferences in Android. The storage location of the created Preferences file can be viewed in Eclipse:
DDMS-> File Explorer/ /Shared_prefs/setting. xml
Demoprivate void toastDisplay (){
// Display prompt control for the first time
Preferences = getSharedPreferences ("count", 0); // if it exists, open it; otherwise, create a new Preferences.
Int count = preferences. getInt ("count", 0); // retrieve data
If (count = 0) {// judge the program and the number of runs. if this is the first run, the page jumps to the boot page.
ToastInit ();
SharedPreferences. Editor editor = preferences. edit (); // make preferences editable
Editor. putInt ("count", 1); // store data
Editor. commit (); // submit the modification
}
}
Private void toastInit (){
// Initially use the toast prompt to set the gesture
Toast toast = Toast. makeText (this,
"Please set your gesture for first used", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER_HORIZONTAL | Gravity. BOTTOM, 0, 0 );
Toast. setMargin (0f, 0.1f );
Toast. show ();
}
Ref:
Http://blog.csdn.net/pipisorry/article/details/26874145
Http://blog.csdn.net/wxyyxc1992/article/details/17222841
Http://blog.csdn.net/t80t90s/article/details/7925541
Http://blog.csdn.net/listening_music/article/details/6611786