In our development, the application will save a small amount of data, such as some strings, some tags or some configuration files, if you go to use SQLite to save the data, it will inevitably appear overqualified, it is inconvenient to use, for this information, Saved in the sharedpreferences is suitable.
I. Brief introduction of Sharedpreferences.
1, Sharedpreferences is a lightweight storage method, its principle is based on XML file storage Key-value key value pairs of data, usually used to store some simple configuration information. Storage location:/data/data/{Package name}/shared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object.
2, Sharedpreferences object and SQLite database, compared to the more lightweight, do not need to create a database, create tables, write SQL statements and a series of operations. The drawback is that sharedpreferences only supports several types of data:
Boolean, int,float, long,String
3, sharedpreferences itself is an interface, the program cannot directly create its instance, only through the context provided by the Getsharedpreferences (String name, int mode) to get its instance, The second parameter of the method supports several values:
· Context.mode_private: Only read and write by local program.
· Context.mode_world_readable: Can be read by other programs.
· Context.mode_world_writeable: Can be read and written by other programs.
4, sharedpreferences commonly used methods are as follows:
· Boolean Contans (String key): Determines whether the data in the sharedpreferences contains a specific key.
· Abstract map<string,?> getAll (): Gets all the Key-value key-value pairs.
· Boolean getXxx (String key, xxx default): Gets the value corresponding to the specified key, and returns the default value if the key does not exist.
Another point is that it does not support conditional queries and so on, so although the sharedpreferences is very convenient to operate, it can only be used as a supplement to the storage method, but not completely replace the SQLite database and other data storage methods.
Ii. Introduction of Editor
Instead of providing a way to write data, the sharedpreferences itself calls the editor () method through the Sharedpreferences object to get the editor object to write the data. The common methods of editor are as follows:
· Clear (): Empties all data in the sharedpreferences.
· Putxxx (String key, XXX value): Writes data to sharedpreferences.
· Remove (String key): Removes the value of the specified key in Sharedpreferences.
· Commit (): When editor finishes editing, the method commits the modification.
Iii. Use an example to illustrate the use of sharedpreferences
Here's a simple example of how sharedpreferences is used.
1, first create a layout file Activity_main, while in the layout file to place four space, namely: input box, read, write and clear. Read the code as follows:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 >7 8 <EditText9 Android:id= "@+id/et_data"Ten Android:layout_width= "Wrap_content" One Android:layout_height= "Wrap_content" A Android:ems= "Ten" > - - <Requestfocus/> the </EditText> - - <Button - Android:id= "@+id/btn_read" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:text= "read" /> A at <Button - Android:id= "@+id/btn_write" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "Write" /> - in <Button - Android:id= "@+id/btn_clear" to Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" - Android:text= "Clear" /> the * </LinearLayout>
The layout file is very simple and is not analyzed here.
2, then create Mainactivity, and in the mainactivity implementation of Sharedpreferences write, read, clear and other related operations, the code is as follows:
1 /**2 * mainactivity3 * 4 * @authorLiuwei5 * 6 */7 Public classMainactivityextendsActivity {8 Privatesharedpreferences preferences;9 PrivateEditor editor;Ten EditText Et_data; One Button Btn_read; A Button Btn_write; - Button btn_clear; - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); +Et_data =(EditText) Findviewbyid (r.id.et_data); -Btn_read =(Button) Findviewbyid (r.id.btn_read); +Btn_write =(Button) Findviewbyid (r.id.btn_write); ABtn_clear =(Button) Findviewbyid (r.id.btn_clear); atPreferences =getpreferences (activity.mode_private); -Editor =Preferences.edit (); -Btn_read.setonclicklistener (NewOnclicklistener () { - - @Override - Public voidOnClick (View v) { inString str = preferences.getstring ("Key", "no Data"); - Toast.maketext (Getapplicationcontext (), str, toast.length_short) to . Show (); + } - }); theBtn_write.setonclicklistener (NewOnclicklistener () { * $ @OverridePanax Notoginseng Public voidOnClick (View v) { -Editor.putstring ("Key", Et_data.gettext (). toString ()); the if(Editor.commit ()) { +Toast.maketext (Getapplicationcontext (), "Write succeeded", A toast.length_short). Show (); the } + } - }); $Btn_clear.setonclicklistener (NewOnclicklistener () { $ - @Override - Public voidOnClick (View v) { the editor.clear (); - if(Editor.commit ()) {WuyiToast.maketext (Getapplicationcontext (), "Purge succeeded", the toast.length_short). Show (); - } Wu - } About }); $ } -}
Code Analysis:
(1) The Sharedpreferences and editor objects are first created in the code, and they are initialized in the OnCreate () method.
(2) The Editor.putstring () method is written in the form of a key-value pair when the data is written.
(3) through the Sharedpreferences object's GetString (key, default) method, you can get the value of the corresponding key, here the second parameter is the default value in the absence of the key corresponding to the data, we can assign the value by ourselves.
(4) Finally, the sharedpreferences can be emptied by the Editor.clear () method.
Note: The operation of the Editor object requires the execution of the commit method. At the same time the Editor.commit () method returns a Boolean value that executes successfully returns TRUE, and the execution failure returns false.
This article link: http://www.cnblogs.com/codingblock/p/4887436.html
Disclaimer: This article welcomes any form of reprint, but please be sure to keep this article link, thank you!
Android Learning Note (13) Sharedpreference must master the basics