Overview
The sharedpreferences is simple to use and can easily store data and read data. sharedpreferences can save only simple types of data, such asString,int , and so on. It is common to convert data of complex types to Base64 encoding, and then save the converted data as a string in an XML file, and then use the Sharedpreferences Save.
use sharedpreferences save key-value
1 ) use activity getsharedpreferences method get sharedpreferences key-value The name of the file is determined by the getsharedpreferences The first parameter of a method is specified.
2 ) use sharedpreferences interface edit get sharedpreferences.editor object.
3 ) through sharedpreferences.editor interface putxxx method Save key-value Yes. where xxx represents different data types. For example: String type value needed putstring method.
(4) save Key-value by the commit method of the sharedpreferences.editor interface that 's right. the Commit method is equivalent to a commit operation in a database transaction.
Use steps
The writing process for the specific code is:
A, storage data information
1.Open Preferences, name is setting, if present, open it, otherwise create a new Preferences
Sharedpreferences Settings = getsharedpreferences("Setting", 0);
2, let setting in the editing state
Sharedpreferences. Editor Editor = settings. Edit();
3. Store Data
Editor. Putstring("name","Ataaw");
Editor. Putstring("URL","Ataaw". COM ");
4. Complete the submission
Editor. Commit();
B, read data information
1. Get Preferences
Sharedpreferences Settings = getsharedpreferences("Setting", 0);
2. Take out the data
String name = settings. GetString("name"," default value ");
String URL = setting. GetString("URL","Default");
// above is android file storage location can be in eclipse view:
DDMS-File Explorer /< package name>/shared_prefs/setting . Xml
Demoprivate void Toastdisplay () {
First Use Display prompt control
Preferences = Getsharedpreferences ("Count", 0); exists then open it, otherwise create a new preferences
int count = Preferences.getint ("Count", 0); Remove data
if (count = = 0) {//Judge the program with the first run, or jump to the boot page if the
Toastinit ();
Sharedpreferences.editor Editor = Preferences.edit (); Make preferences in edit state
Editor.putint ("Count", 1); Deposit data
Editor.commit (); Submit Changes
}
}
private void Toastinit () {
Initial use display Toast prompt set 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