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"; /** * saving data to a file * @param context * @param key * @param data */ public static void SaveData ( Context context, String key,object 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 (); } /** * reading data from a file * @param context * @param key * @param defvalue * @return */ public Static Object GetData (context context, String key, Object defvalue) { string type = Defvalue.getclass (). Getsimplename (); shareDpreferences sharedpreferences = context.getsharedpreferences (file_name, context.mode_private); // Defvalue is the default value, and returns it if the data is not currently 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 Common Tool Class package---sharedpreferencesutil