ImportAndroid.content.Context;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;/*** Sharedpreferences is a lightweight way of storing data, which is essentially based on XML files that store Key-value key-value pairs of data,<br/> * typically used to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory. <br/> * The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object. <br/> * Sharedpreferences objects are easier to use than the SQLite database, such as creating a database, creating tables, writing SQL statements, and more. <br/> * But sharedpreferences only supports the following data types:<br/> * boolean<br/> * int<br/> * float<br/> * long& lt;br/> * string<br/> * But cannot perform conditional queries, etc., so no matter how simple the sharedpreferences data store operation is,<br/> * It can only be a supplement to the storage method, Instead of completely replacing other data storage methods such as SQLite databases. <br/> * *@authorWangzhu * @date November 22, 2014 morning 10:17:49*/ Public classSharedpreferencesutils {/*** Write content to sharedprefrences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramValue * Values*/ Public Static voidWrite (context context, string fileName, string key,Booleanvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.putboolean (key, value); Editor.commit (); } /*** Read the value corresponding to the tag in the sharedpreferences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramDefvalue * Default value *@return */ Public Static BooleanReadboolean (context context, string fileName, string key,Booleandefvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); returnPreferences.getboolean (key, defvalue); } /*** Write content to sharedprefrences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramValue * Values*/ Public Static voidWrite (context context, string fileName, string key,intvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.putint (key, value); Editor.commit (); } /*** Read the value corresponding to the tag in the sharedpreferences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramDefvalue * Default value *@return */ Public Static intreadInt (context context, string fileName, string key,intdefvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); returnpreferences.getint (key, defvalue); } /*** Write content to sharedprefrences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramValue * Values*/ Public Static voidWrite (context context, string fileName, string key,Longvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.putlong (key, value); Editor.commit (); } /*** Read the value corresponding to the tag in the sharedpreferences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramDefvalue * Default value *@return */ Public Static LongReadlong (context context, string fileName, string key,Longdefvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); returnPreferences.getlong (key, defvalue); } /*** Write content to sharedprefrences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramValue * Values*/ Public Static voidWrite (context context, string fileName, string key,floatvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.putfloat (key, value); Editor.commit (); } /*** Read the value corresponding to the tag in the sharedpreferences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramDefvalue * Default value *@return */ Public Static floatReadfloat (context context, string fileName, string key,floatdefvalue) {Sharedpreferences Preferences=context.getsharedpreferences (FileName, context.mode_private); returnpreferences.getfloat (key, defvalue); } /*** Write content to sharedprefrences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramValue * Values*/ Public Static voidWrite (context context, string fileName, String key, String value) {Sharedpreferences preferences /c0>=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.putstring (key, value); Editor.commit (); } /*** Read the value corresponding to the tag in the sharedpreferences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag *@paramDefvalue * Default value *@return */ Public Staticstring readString (context context, string fileName, String key, String defvalue) {Sharedpreference S Preferences=context.getsharedpreferences (FileName, context.mode_private); returnpreferences.getstring (key, defvalue); } /*** Delete the value corresponding to key in Sharedpreferences * *@paramContext * Contextual Object *@paramFileName * File name *@paramKey * Tag*/ Public Static voidRemove (context context, string fileName, String key) {Sharedpreferences preferences=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.remove (key); Editor.commit (); } /*** Clear all contents of the corresponding file in Sharedpreferences * *@paramContext * Contextual Object *@paramFileName * file name*/ Public Static voidClear (Context context, String FileName) {sharedpreferences preferences=context.getsharedpreferences (FileName, context.mode_private); Editor Editor=Preferences.edit (); Editor.clear (); Editor.commit (); }}
The sharedpreferences of data storage in Android