There are four basic methods of data preservation in Android, one is Sharedpreference, two is file, three is SQLite, four is contentprovider. Sharedpreferences is widely used as a simple and simple way to save configuration information for your application.
Sharedpreferences is a way to store configuration information for an application in key-value pairs , which can only store basic data types. A program's configuration file can only be used in this application, or can only be used within the same package, and cannot be used between different packages . In fact Sharedpreferences is the use of XML format to store data in the device, in Ddms in the file Explorer /data/data/<package name>/shares_prefs the next.
writing data to Sharedpreferences
Java code
- Sharedpreferences settings = getsharedpreferences (Prefs_name, 0);
- Sharedpreferences.editor Editor = Settings.edit ();
- Editor.putboolean ("Silentmode", Msilentmode);
- Editor.commit (); //Be sure to remember to submit
reading data from the Sharedpreferences
Java code
- Sharedpreferences settings = getsharedpreferences (Prefs_name, 0);
- //or using sharedpreferences settings = getpreferences (0);
- Boolean silent = Settings.getboolean ("Silentmode", false);
Function Description:
1, android.content.Context.getSharedPreferences (String name, int mode)
Returns the contents of the Preferences file ' name '. There is only one sharedpreferences for a name, so any modifications can be understood to take effect. The name Preferences file name MODE defaults to 0 (mode_private) to indicate that the preferences file is private to the app, and only this app can accessmode_world_readableIndicates that all other programs have Read permissionsmode_world_writeableIndicates that all other programs have write permissions
2, android.app.Activity.getPreferences (int mode)Returns the currentactivity-Private sharedpreferences. Equivalent to the above getsharedpreferences (String, int) method name passed in as the class name of the current activity
3, Android.preference.Preference.getSharedPreferences ()
Returns the sharedpreferences that the current preference can access
The sharedpreference of "Android Sunflower Treasure Book"