1 /** 2 * Sharedpreferences of a tool class, call SetParam can save string, Integer, Boolean, Float, long type parameters3 * Also call GetParam to get the data stored in the phone4 * @authorxiaanming5 * 6 */ 7 Public classSharedpreferencesutils {8 /** 9 * file name stored in the phoneTen */ One Private Static FinalString file_name = "Share_date"; A - - /** the * method of saving data, we need to get the specific type of the data to be saved, then call different save methods according to the type - * @paramContext - * @paramKey - * @paramObject + */ - Public Static voidSetParam (context context, String key, Object object) { + AString type =Object.getclass (). Getsimplename (); atSharedpreferences SP =context.getsharedpreferences (file_name, context.mode_private); -Sharedpreferences.editor Editor =Sp.edit (); - - if("String". Equals (Type) { - editor.putstring (Key, (String) object); - } in Else if("Integer". Equals (Type) { - Editor.putint (Key, (Integer) object); to } + Else if("Boolean". Equals (Type) { - Editor.putboolean (Key, (Boolean) object); the } * Else if("Float". Equals (Type) { $ editor.putfloat (Key, (Float) object); Panax Notoginseng } - Else if("Long". Equals (Type) { the Editor.putlong (Key, (Long) object); + } A the Editor.commit (); + } - $ $ /** - * Get the method to save the data, we are based on the default value to save the specific type of data, and then call the relative method to get the values - * @paramContext the * @paramKey - * @paramDefaultobjectWuyi * @return the */ - Public StaticObject GetParam (context context, String key, Object defaultobject) { WuString type =Defaultobject.getclass (). Getsimplename (); -Sharedpreferences SP =context.getsharedpreferences (file_name, context.mode_private); About $ if("String". Equals (Type) { - returnsp.getstring (Key, (String) defaultobject); - } - Else if("Integer". Equals (Type) { A returnSp.getint (Key, (Integer) defaultobject); + } the Else if("Boolean". Equals (Type) { - returnSp.getboolean (Key, (Boolean) defaultobject); $ } the Else if("Float". Equals (Type) { the returnsp.getfloat (Key, (Float) defaultobject); the } the Else if("Long". Equals (Type) { - returnSp.getlong (Key, (Long) defaultobject); in } the the return NULL; About } the}
Save data:
Sharedpreferencesutils.setparam (This, "String", "xiaanming"); Sharedpreferencesutils.setparam (this, "int", ten); Sharedpreferencesutils.setparam (Thistrue); Sharedpreferencesutils.setparam (This, "long", 100L); Sharedpreferencesutils.setparam (This, "float", 1.1f);
Get Data:
1 Sharedpreferencesutils.getparam (timeractivity.this , "String", ""); Sharedpreferencesutils.getparam (Timeractivity. This , "int", 0); 2 Sharedpreferencesutils.getparam (timeractivity.this , "boolean", false ); 3 Sharedpreferencesutils.getparam (timeractivity.this , "Long", 0l); 4 Sharedpreferencesutils.getparam (timeractivity.this , "float", 0.0f);