Android apps There are four ways to store data locally, in a lightweight to heavyweight order:
1. Storing data with sharedpreferences
2. Storing data with ContentProvider
3. Store data with SQLite
4. Store data with local files
If you add a network to store data, there are five of them, first we look at the first kind of sharedpreference:
First, interface android.content.SharedPreferences
Its properties are similar to the INI configuration file for earlier Windows, and are ultimately saved in XML, and you can also understand it as a small key-value database.
Sharedpreference is essentially an interface that uses the Dalvik underlying xmlparser to process XML, and resource consumption seems to be not particularly large. Let's look at the documentation for the Android SDK to say:
Interface for accessing and modifying preference data returned by Context.getSharedPreferences . For any particular set of preferences, there are a single instance of this class, and all clients share. Modifications to the preferences must go through an Editor object to ensure the preference values remain in a consistent s Tate and control when they is committed to storage. Objects that is returned from the various get methods must is treated as immutable by the application.
Note:currently This class does not the support use across multiple processes. This would be added later.
Translation:
It is an interface for accessing and changing the preferences (or preferences, attributes, configurations, whatever you say) returned by Context.getsharedprenference . For any particular set of properties, there is a singleton shared by all clients. If you want to change these properties, you must use an Editor (Sharedpreferences's inner Class) object to ensure that the attribute values are kept in a consistent and controlled state when they are committed to the storage device. The program must handle objects returned in the Get method in a immutable (immutable) manner.
Note: This class does not currently support cross-process access, and this feature will be incremented later.
Four steps to using sharedpreferences:
1. Get the context object
2, using the Getsharedpreferences method of the context object to get a Sharedpreference object , the following is the Android SDK documentation:
Public Abstract Sharedpreferences getsharedpreferences (String name, int mode)
Retrieve and hold the contents of the Preferences file ' name ', returning a sharedpreferences through which you can Retriev E and modify its values. Only one instance of the Sharedpreferences object was returned to any callers for the same name, meaning they would see each Other ' s edits as soon as they is made.
Parameters
| Name |
Desired Preferences file. If a preferences file by the this name is does not exist, it'll be created when you retrieve an editor (Sharedpreferences.edit ( ) and then Commit changes (Editor.commit ()). |
| Mode |
Operating mode. Use 0 or MODE_PRIVATE for the default operation, and to MODE_WORLD_READABLE MODE_WORLD_WRITEABLE control permissions. |
Returns
- The single
SharedPreferences instance the can is used to retrieve and modify the preference values.
3. Use the edit () method of the Sharedpreference object to get an editor object (this step can be omitted if you do not need to write)
4. Get a specific preference attribute value
5, using the Editor.putxxx () method to modify the property value, XXX is a type name (such as Long, Int, string, etc.)(if you do not need to write, this step can be omitted)
6. Commit the changes with the Editor.commit () method (This step can be omitted if you do not need to write)
Here is the sample code:
Import Android.app.activity;import android.os.bundle;import Android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.content.context;import Android.content.sharedpreferences;import android.content.sharedpreferences.editor;/** * @author Chandlerpublic class Testpref extends Activity { Sharedpreferences mysharedpreferences; Editor Editor;long oldvalue,newvalue = 1234; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_interval_compare);//Get Context object Context CTX = testpref.this;//gets the Sharedpreference object, test is the property file name mysharedpreferences = ctx.getsharedpreferences ("Test", MODE_ PRIVATE);//Get Editor Object Editor = Mysharedpreferences.edit ();//read old value stored in file, if not present, return -1oldvalue = Mysharedpreferences.getlong ("OldValue",-1);//output read OldValue value System.out.println (oldValue) in Logcat;} Save @overrideprotected Void OnDestroy () {Super.ondestroy () when exiting, and/or store long newvalue in oldvalue to Editor.putlong (" OldValue ", newvalue);//Submit Change EDITor.commit (); LOG.D (TAG, "----ondestroy----");}}
The above code is not debug, you can run to see, the rest of the free and more
How Android Apps store local data