I wrote an SP helper class.
Although the writing is a bit verbose, but also their own results. As follows:
Package Com.yqy.yqy_testsputil;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.context;import android.content.sharedpreferences;/** * SP Helper class * @author YQY * * */@SuppressLint (" Commitprefedits ") public class Sputil {private static sharedpreferences msp;private static String spname =" Yqy_testsputil ";/** * put * @param context * @param name key * @param object value */public static void put (context context,string name,obj ECT object) {MSP = Context.getsharedpreferences (spname, activity.mode_private); Sharedpreferences.editor Editor = Msp.edit (); if (object instanceof String) {editor.putstring (name, (String) object);} else if (object instanceof Integer) {editor.putint (name, (Integer) object);} else if (object instanceof Long) {Editor.putlong (name, (Long) object);} else if (object instanceof Float) {editor.putfloat (name, (Float) object);} else if (object instanceof Boolean) {Editor.putboolean (name, (Boolean) object);} Editor.commit ();} public static void Set (context context, string what, String value) {sharedpreferences sharedpreferences = context.getsharedpreferences (spname, 0); Sharedpreferences.edit (). putstring (What, Value). commit ();} public static String getString (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_ PRIVATE); return msp.getstring (Name, "");} public static int getInt (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_private ); return msp.getint (name, 0);} public static Long Getlong (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_ PRIVATE); return Msp.getlong (name, 0);} public static Float getfloat (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_ PRIVATE); return msp.getfloat (name, 0);} public static Boolean Getboolean (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode _private); return Msp.getboolean (name, false);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android uses sharedpreferences data storage