This tool class permanent maintenance, permanent update, if you find that there are bugs or unreasonable, please leave a message, Bo will correct the first time.
This tool class provides the following features:
1. Storage of five types of data;
2. Read five types of data;
The contents are as follows:
Import Android.content.context;import Android.content.sharedpreferences;import android.content.sharedpreferences.editor;/** * * @author Bear * * Sharedprefereces Tool class * */public class SHAREDPR Efsutil {/** * writes int type data to sharedpreferences * * @param context Context * @param name for XML file * @param key * @param val UE value */public static void Putvalue (context context, string name, string Key,int value) {Editor SP = geteditor (context, NAM e); Sp.putint (key, value); Sp.commit ();} /** * Writes a Boolean type of data to sharedpreferences * * @param context context * @param name corresponding to the XML filename * @param key key * @param value */public static void Putvalue (context context, string name, string Key,boolean value) {Editor SP = geteditor (context, NAM e); Sp.putboolean (key, value); Sp.commit ();} /** * Writes a string type of data to sharedpreferences * * @param context context * @param name corresponding to the XML filename * @param key key * @param value */public static void Putvalue (context context, string name, string key,string value) {Editor sp = Geteditor (cOntext, name); sp.putstring (key, value); Sp.commit ();} /** * writes float type data to sharedpreferences * * @param context context * @param name corresponding to the XML filename * @param key key * @param value * /public static void Putvalue (context context, string name, string key,float value) {Editor SP = geteditor (context, name); s P.putfloat (key, value); Sp.commit ();} /** * Writes a long type of data to the sharedpreferences * * @param context context * @param name corresponding XML filename * @param key key * @param value * * public static void Putvalue (context context, string name, string Key,long value) {Editor SP = geteditor (context, name); Sp. Putlong (key, value); Sp.commit ();} /** * Reads data of type int from Sharedpreferences * * @param context context * @param name corresponding to XML filename * @param key key * @param defvalue if Read unsuccessful Use default value * @return return read value */public static int GetValue (context context, string name, String Key,int defvalue) {SHAREDPR Eferences SP = getsharedpreferences (context, name); int value = Sp.getint (key, defvalue); return value;} /** * Read Boolean-type data from Sharedpreferences * *@param context Context * The XML file name corresponding to the @param name * @param key key * @param defvalue if the read is unsuccessful then use the default value * @return return the Read value */public STA Tic Boolean GetValue (context context, string name, String Key,boolean defvalue) {sharedpreferences sp = Getsharedpreferenc ES (context, name); Boolean value = Sp.getboolean (key, defvalue); return value;} /** * Reads a string type of data from Sharedpreferences * * @param context context * @param name corresponding to the XML filename * @param key key * @param defvalue If the read is unsuccessful then use the default value * @return return the Read value */public static string GetValue (context context, string name, String key,string defvalue) {Sharedpreferences sp = getsharedpreferences (context, name); String value = sp.getstring (key, defvalue); return value;} /** * Read data of type float from sharedpreferences * * @param context context * @param name corresponding to XML filename * @param key * @param defvalue If the read is unsuccessful then use the default value * @return return the Read value */public static float GetValue (context context, string name, String Key,float defvalue) {Sh Aredpreferences SP = getsharedpreferences (context, name); float value = sp.GetFloat (key, defvalue); return value;} /** * Reads a long type of data from Sharedpreferences * * @param context context * @param name corresponding to the XML filename * @param key key * @param defvalue as If the read is unsuccessful, use the default value * @return return the Read value */public static long GetValue (context context, string name, String Key,long defvalue) {Share Dpreferences SP = getsharedpreferences (context, name); Long value = Sp.getlong (key, defvalue); return value;} Gets the editor instance private static Editor Geteditor (context context, String name) {return getsharedpreferences (context, name). Edit ();} Get sharedpreferences instance private static Sharedpreferences getsharedpreferences (context context, String name) {return Context.getsharedpreferences (name, context.mode_private);}}
If your project supports version information for Android API level >= 11, then you can also add set<string> type of storage and read.
Examples of how to invoke activity or other places are as follows:
Store data, store key-value pairs in the mysetting file ("Color", "Red") Sharedprefsutil.putvalue (This, "mysetting", "Color", "red");
Reads the data from the MySetting file, reading the value of the key "color" As String color = Sharedprefsutil.getvalue (This, "mysetting", "Color", "blue");
Android Sharedpreferences Tool class