Sharedpreferences is often used to save simple data, such as recording the configuration of user operations, and easy to use.
public class Sharedpreferencesutil {//stored sharedpreferences file name private static final String file_name = "Save_file_name" /** * Save data to File * @param context * @param key * @param data */public static void SaveData (context context, String Key,objec T data) {String type = Data.getclass (). Getsimplename (); Sharedpreferences sharedpreferences = context.getsharedpreferences (file_name, context.mode_private); Editor editor = Sharedpreferences.edit (), if ("Integer". Equals (Type)) {Editor.putint (key, (Integer) data);} else if ("Boolean". Equals (Type)) {Editor.putboolean (Key, (Boolean) data);} else if ("string". Equals (Type)) {editor.putstring (key, (String) data);} else if ("float". Equals (Type)) {Editor.putfloat (key, (Float) data);} else if ("Long". Equals (Type)) {Editor.putlong (key, (long) data);} Editor.commit ();} /** * Read data from a file * @param context * @param key * @param defvalue * @return */public static Object getData (context context, S Tring Key, Object defvalue) {String type = Defvalue.getclass (). Getsimplename (); SharedpreferencEs sharedpreferences = context.getsharedpreferences (file_name, context.mode_private);//defvalue is the default value, Assuming that the data is not currently fetched, return it if ("Integer". Equals (Type)) {return Sharedpreferences.getint (key, (Integer) defvalue);} else if ("Boolean". Equals (Type)) {return Sharedpreferences.getboolean (Key, (Boolean) defvalue);} else if ("string". Equals (Type)) {return sharedpreferences.getstring (key, (String) defvalue);} else if ("float". Equals (Type)) {return sharedpreferences.getfloat (key, (Float) defvalue);} else if ("Long". Equals (Type)) {return Sharedpreferences.getlong (key, (long) defvalue);} return null;}}
Android often uses the tool class package---sharedpreferencesutil