Today is free, the file configuration Sharepreference write a package of demo good tidy up.
First look at the definition of it on the Internet:
Sharedpreferences is the easiest data storage technology to understand in Android, and in fact sharedpreferences is dealing with a key-value (key-value pair) Sharedpreferences used to store some lightweight data.
Encapsulated class
package com.example.testlistq;import android.content.context;import Android.content.sharedpreferences;public class sharedpreferencesutils {private static final String FILE_NAME = "Share_date";/** * the method of saving data, we need to get the specific type of data to be saved, Then call different save methods based on the type * * @param context * @param key Save the corresponding key * @param object Save the corresponding value */public static void setparam (Context context, string key, object object) {String type = Object.getclass (). Getsimplename (); Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private); Sharedpreferences.editor editor = sp.edit ();if ("String". Equals (type)) { Editor.putstring (key, (String) object);} else if ("integer". Equals (Type)) {editor.putint (key, (integer) object);} else if ("Boolean". Equals (Type)) {editor.putboolean (key, (Boolean) object);} else if ("float". Equals (Type)) {editor.putfloat (key, (float) object);} else if ("Long". Equals (Type)) {editor.putlong (key, (long) object);} Editor.commit ();} /** * get the method to save the data, we are based on the specific type of data that is worth saving, and then call the relative method to get the value * * @param context * @param key * @param defaultObject * @return */public static object getparam (Context context, string key, object defaultobject) { String type = defaultobject.getclass (). Getsimplename (); Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private); ("string". Equals (Type)) {return sp.getstring (key, (string) defaultobject);} else if ("Integer". Equals (Type)) {return sp.getint (Key, (Integer) defaultobject);} else if ("Boolean". Equals (Type)) {return sp.getboolean (key, (Boolean) Defaultobject);} else if ("float". Equals (Type)) {return sp.getfloat (key, (float) Defaultobject);} else if ("Long". Equals (Type)) {return sp.getlong (key, (Long) defaultobject );} Return null;}}
Invoke instance
Sharedpreferencesutils.setparam (This, "name", "Xiaoming"); Sharedpreferencesutils.setparam (this, "id", 10241); Sharedpreferencesutils.setparam (This, "Ifold", true); String str = Sharedpreferencesutils.getparam (this, "name", "name"). ToString (); String id = sharedpreferencesutils.getparam (this, "id", 123). ToString (); String old = Sharedpreferencesutils.getparam (this, "Ifold", false). ToString (); Toast.maketext (this, id + "," + str + "," + old, Toast.length_long). Show ();
This article from "Climb over the mountains to see the sea" blog, declined to reprint!
Sharedpreferences Package Class