Import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;import Java.util.Map;import Android.content.context;import Android.content.sharedpreferences;public class Sputils{public SPUtils () {/* cannot be instantiated */throw New Unsupportedoperationexception ("cannot be instantiated"); /** * The file name stored in the phone */public static final String file_name = "Share_data";/** * method of saving data, we need to get the specific type of the data to be saved, then call different save method according to the type * * @param context * @param key * @param object */public static void put (context context, String key, Object object) {SHAREDPR Eferences sp = context.getsharedpreferences (file_name,context.mode_private); Sharedpreferences.editor Editor = Sp.edit (); if (object instanceof String) {editor.putstring (key, (String) object);} else If (object instanceof Integer) {Editor.putint (key, (Integer) object);} else if (object instanceof Boolean) { Editor.putboolean (Key, (Boolean) object);} else if (object instanceof Float) {editor.putfloat (key, (Float) object);} else if (object instanceof LoNG) {Editor.putlong (key, (Long) object),} else{editor.putstring (Key, object.tostring ());} Sharedpreferencescompat.apply (editor);} /** * Get the method to save the data, we based on the default value to save the specific type of data, and then call the relative method to get the values * * @param context * @param key * @param defaultobject * @return */publ IC Static Object Get (context context, String key, Object defaultobject) {sharedpreferences sp = Context.getsharedpreferenc ES (file_name,context.mode_private), if (Defaultobject instanceof String) {return sp.getstring (key, (String) Defaultobject);} else if (defaultobject instanceof Integer) {return Sp.getint (key, (Integer) defaultobject);} else if (Defaultobject Instan Ceof Boolean) {return Sp.getboolean (key, (Boolean) defaultobject),} else if (Defaultobject instanceof Float) {return Sp.getfloat (Key, (Float) defaultobject);} else if (defaultobject instanceof long) {return Sp.getlong (key, (Long) defaultobject);} return null;} /** * Remove the value already corresponding to a key value * * @param context * @param key */public static void Remove (context context, String key) {Sharedprefe Rences sp = context.getsharedpreferences (file_name,context.mode_private); Sharedpreferences.editor Editor = Sp.edit (); Editor.remove (key); Sharedpreferencescompat.apply (editor);} /** * Clear All data * * @param context */public static void clear (context context) {Sharedpreferences sp = Context.getsharedprefe Rences (file_name,context.mode_private); Sharedpreferences.editor Editor = Sp.edit (); Editor.clear (); Sharedpreferencescompat.apply (editor);} /** * Query If a key already exists * * @param context * @param key * @return */public Static Boolean contains (context context, String Ke Y) {Sharedpreferences sp = context.getsharedpreferences (file_name,context.mode_private); return Sp.contains (key);} /** * Returns all key value pairs * * @param context * @return */public static map<string,?> GetAll (context context) {Sharedpreference s SP = context.getsharedpreferences (file_name,context.mode_private); return Sp.getall ();} /** * Create a compatible class that resolves the sharedpreferencescompat.apply method * * @author Zhy * */private Static class Sharedpreferencescompat{priva Te Static Final Method Sapplymethod = Findapplymethod ();/** * Reflection Find the method of apply * * @return */@SuppressWarnings ({"Unchecked", "Rawtyp Es "}) private static Method Findapplymethod () {Try{class CLZ = Sharedpreferences.editor.class;return Clz.getmethod (" Apply ");} catch (Nosuchmethodexception e) {}return null;} /** * If found, use apply to execute, otherwise use commit * * @param editor */public static void apply (Sharedpreferences.editor editor) {try{if (sAp Plymethod = null) {Sapplymethod.invoke (editor); return;}} catch (IllegalArgumentException e) {} catch (Illegalaccessexception e) {} catch (InvocationTargetException e) {} Editor.commit ();}}}
Android Tool class------->sharedpreferences