In the next four articles, we'll cover four ways to store data in Android:
- Sharedpreferences
- Sqlite
- Files
- Internet
Today we'll look at one of the simplest: sharedpreferences.
This data storage method is the simplest, lightest, and most practical, but can only be used to store basic data types. Let's take a look at how to use:
1. Res/layout/activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http ://schemas.android.com/tools " android:layout_width=" Match_parent " android: layout_height= "Match_parent" android:orientation= "vertical" tools:context= ". Mainactivity "> <button Android:id= "@+id/btn_putvalue" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Add value"/> <button Android:id= "@+id/btn_get" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Get current SP value"/> <button android:id= "@+id/btn_remove" android:layout_width= "wrap_content " android:layout_height=" Wrap_content " android:text= "Remove the value of key"/> <button android:id= "@+id/btn_clear" android:layout_width= " Wrap_content " android:layout_height=" Wrap_content " android:text= "Clear all data"/></linearlayout>
2. Mainactivity.java
Package Com.example.ch6_01_sharepreferences;import Java.util.map;import Android.os.bundle;import Android.app.activity;import Android.content.intent;import Android.content.sharedpreferences;import Android.util.log;import Android.view.menu;import Android.view.view;import Android.widget.button;public class Mainactivity extends Activity { private static final String TAG = "mainactivity"; @Ov erride protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview (R.layout.activity_main); button btn_getsp = (Button) Findviewbyid ( R.id.btn_get); btn_getsp.setonclicklistener (New View.onclicklistener () { @Override &NBSp;public void OnClick (View v) { Sharedpreferences sp = getpreferences (mode_private); map<string, object> Map = (map<string, object>) sp.getall (); & nbsp; log.e (TAG, "" +map); String playmusic = sp.getstring ("Playmusic", ""); log.e (TAG, "Playmusic:" +playmusic); } }); button btn_putvalue = (Button) Findviewbyid (r.id.btn_putvalue); btn_putvalue.setonclicklistener (New View.onclicklistener () { &NBSP;&NBSP;&NBsp @Override public void OnClick (View V ) { sharedpreferences sp = getpreferences ( mode_private); sharedpreferences.editor ET = Sp.edit (); et.putboolean ("Autologin" , true); et.putstring ("Playmusic", "true" ); et.commit (); } }); button btn_remove = (Button) Findviewbyid (r.id.btn_remove); & nbsp; btn_remove.setonclicklistener (New View.onclicklistenER () { @Override public void OnClick (View v) { Sharedpreferences sp = getpreferences (mode_private); sharedpreferences.editor et = Sp.edit (); & nbsp et.remove ("Autologin"); et.commit (); } }); button btn_clear = (Button) Findviewbyid (r.id.btn_ Clear); btn_clear.setonclicklistener (New View.onclicklistener () { @Override public void OnClick (View v) { sharedpreferences sp = getpreferences (mode_private); sharedpreferences.editor et = Sp.edit (); & nbsp; et.clear (); et.commit (); } }); } @Override public boolean Oncreateoptionsmenu (Menu menu) { //inflate the menu; this adds items To the action Bar if it is present. getmenuinflater (). Inflate (R.menu.main, menu); & nbsp; return true; }}
Let's start with a brief introduction to the Sharedpreferences, which is the Android package, dedicated storage preferences, the underlying is an XML file, the directory is stored in: data/data/application package name/shared_prefs/directory, Of course, this file processing do not need you to do, are Android to complete, you have to go is to get Sharedpreferences object, and then add, delete, change, check the data, and then commit () on the line, here why to emphasize commit, Novice use is easy to forget, of course, commit () also contains the idea of a transaction, after all, the file operation, it will take time, so with a transaction to do, foolproof.
Add Data: (Modify the data of course also use this, just fill in the different values on the line)
<pre name= "code" class= "java" >sharedpreferences sp = getpreferences (mode_private); Sharedpreferences.editor et = Sp.edit (); Et.putboolean ("Autologin", true); Et.putstring ("Playmusic", "true"); Et.commit ();
Delete data: (delete the data of the specified key)
Sharedpreferences sp = getpreferences (mode_private); Sharedpreferences.editor et = Sp.edit (); Et.remove ("Autologin"); Et.commit ();
Purge data: (Clears all data in the XML file)
Sharedpreferences sp = getpreferences (mode_private); Sharedpreferences.editor et = Sp.edit (); Et.clear (); Et.commit ();
Get all the data in the XML: (using the GetAll () method to get all the data in the XML file, the exported type is a map, and the visible XML file is stored as a key-value pair)
<pre name= "code" class= "java" >sharedpreferences sp = getpreferences (mode_private); map<string, object> map = (map<string, object>) Sp.getall (); LOG.E (TAG, "+map"); String playmusic = sp.getstring ("Playmusic", "" "); LOG.E (TAG, "Playmusic:" +playmusic);
To get the value of a key, you can use GetString (key, default value). If key is not found, the default value is returned.
Extended:
Well, after watching how to use, we are looking at sharedpreferences generated files, we should note that the method used here to get Sharedpreferences is to call Activity.getpreferences directly (Mode_ PRIVATE), we do not specify the name of the XML file, then it will create an XML file with mainactivity as the file name, there is a picture of the truth:
Of course, when we get sharedpreferences, we can also specify the name of the XML file:
Another way to get sharedpreferences:
Sharedpreferences sputil = getsharedpreferences ("Util", mode_private); Sharedpreferences.editor etutil = Sputil.edit () etutil.putfloat ("temp", 10.2f); Etutil.commit ();
The system will create the Util.xml file in the directory:
Finally we see mode_private this parameter, saying is private, I thought the private meaning is: an activity created by the XML can not be shared by other activity, in fact, refers to other application, other applications can not access. In the same application, as long as the name of the sharedpreferences is right, it is from the shared_prfs/directory down to find files, and all the activity can be interoperable.
Finally let's take a look at what is generated in the XML file, and I export the mainactivity.xml to see what's Inside:
All right, over.
Android Getting Started notes-data storage-sharedpreferences