Android's sharedpreferences Storage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.