Packaged a tool class, directly on the code:
/** * @author jenly * @date 2014-8-8 */public class Sharedpreferencesutils {public static final String pref_name = "Org.ki Ng.pref_name_jenly ";p ublic static sharedpreferences getsharedpreferences (context context) {return Getsharedpreferences (context, pref_name);} public static Sharedpreferences Getsharedpreferences (context context, String Prefname) {return Context.getsharedpreferences (Prefname, context.mode_private);} --------------------------intpublic static void Putint (Context context,string key,int value) {getsharedpreferences ( Context). edit (). Putint (key, value). commit ();} public static int GetInt (context context,string Key,int defvalue) {return getsharedpreferences (context). GETINT (Key, Defvalue);} public static int getInt (Context context,string key) {return getInt (context,key,0);} --------------------------floatpublic static void Putfloat (Context context,string key,float value) { Getsharedpreferences (context). Edit (). Putfloat (key, value). commit ();} public static float GetFloat (context context, String key,float defvalue) {return getsharedpreferences (context). GetFloat (key, defvalue);} public static float GetFloat (context context,string key) {return GetFloat (context, key, 0);} --------------------------longpublic static void Putlong (Context context,string key,long value) { Getsharedpreferences (context). Edit (). Putlong (key, value). commit ();} public static long Getlong (context context,string Key,long defvalue) {return getsharedpreferences (context). Getlong (Key , defvalue);} public static long Getlong (context context,string key) {return Getlong (context, key, 0);} --------------------------booleanpublic static void Putboolean (Context context,string Key,boolean value) { Getsharedpreferences (context). Edit (). Putboolean (key, value). commit ();} public static Boolean Getboolean (context context,string Key,boolean defvalue) {return getsharedpreferences (context). Getboolean (key, defvalue);} public static Boolean Getboolean (Context context,string key) {return Getboolean (context, key, false);} //--------------------------stringpublic static void putstring (context context,string key,string value) {getsharedpreferences (context ). Edit (). putstring (key, value). commit ();} public static String getString (context context,string key,string defvalue) {return getsharedpreferences (context). GetString (key, defvalue);} public static String getString (Context context,string key) {return getString (context, key, NULL);} --------------------------Map@suppresslint ("commitprefedits") public static void Putmapstring (Context context,map <String,String> map) {if (map!=null) {Sharedpreferences.editor Editor = getsharedpreferences (context). edit (); For (entry<string, string> entry:map.entrySet ()) {if (! Textutils.isempty (Entry.getkey ())) editor.putstring (Entry.getkey (), Entry.getvalue ());} Editor.commit ();}} --------------------------Increase int/** * self-increment (default self-increment 1) * @param context * @param key */public static void increase (Contex T context,string key) {Increase (context, key,1);} /** * Self-increment * @param context * @param key * @param dEltavalue increment value (cardinality) */public static void Increase (Context context,string key,int deltavalue) {getsharedpreferences ( Context). edit (). Putint (Key,getint (context, key) +deltavalue). commit ();}}
Android's sharedpreferences Storage