SharedPreferences mSharedPreferences = getSharedPreferences(getPackageName(),MODE_PRIVATE);
存储
SharedPreferences.Editor mEdit = mSharedPreferences.edit();
mEdit.putString("StringData", "Hello String");
mEdit.commit();
读取
String stringData = mSharedPreferences.getString("StringData", "Default");
存储
SharedPreferences.Editor mEdit = mSharedPreferences.edit();
mEdit.putBoolean("booleanData", true);
mEdit.commit();
读取
boolean booleanData = mSharedPreferences.getBoolean("booleanData", false);
存储
SharedPreferences.Editor mEdit = mSharedPreferences.edit();
mEdit.putFloat("floatData", 1.0f);
mEdit.commit();
读取
Float floatData = mSharedPreferences.getFloat("floatData", 0.0f);
存储
SharedPreferences.Editor mEdit = mSharedPreferences.edit();
mEdit.putInt("intData", 1);
mEdit.commit();
读取
int intData = mSharedPreferences.getInt("intData", 0);
存储
SharedPreferences.Editor mEdit = mSharedPreferences.edit();
mEdit.putLong("longData",1L);
mEdit.commit();
读取
long longData = mSharedPreferences.getLong("longData", 0);
package ktalk.kong.qingwei.sharedprifence.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/** * Created by qingwei on 2015/7/20. */
public class SPUtils {
private static SPUtils mSPUtils;
private SharedPreferences mSharedPreferences;
private Editor mEdit;
private SPUtils(Context context) {
mSharedPreferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
mEdit = mSharedPreferences.edit();
}
public static SPUtils getInstance(Context context) {
if (null == mSPUtils) {
mSPUtils = new SPUtils(context);
}
return mSPUtils;
}
/** * String * *********************************************************************** */
public void putString(String key, String value) {
mEdit.putString(key, value).commit();
}
public String getString(String key, String value) {
return mSharedPreferences.getString(key, value);
}
/** * boolean * *********************************************************************** */
public void putBoolean(String key, boolean value) {
mEdit.putBoolean(key, value).commit();
}
public boolean getBoolean(String key, boolean value) {
return mSharedPreferences.getBoolean(key, value);
}
/** * float * *********************************************************************** */
public void putFloat(String key, float value) {
mEdit.putFloat(key, value).commit();
}
public Float getFloat(String key, float value) {
return mSharedPreferences.getFloat(key, value);
}
/** * int * *********************************************************************** */
public void putInt(String key, int value) {
mEdit.putInt(key, value).commit();
}
public int getInt(String key, int value) {
return mSharedPreferences.getInt(key, value);
}
/** * long * *********************************************************************** */
public void putLong(String key, long value) {
mEdit.putLong(key, value).commit();
}
public long getLong(String key, long value) {
return mSharedPreferences.getLong(key, value);
}
}
SPUtils.getInstance(this).putString("stringData", "Hello String");
String stringData = SPUtils.getInstance(this).getString("stringData", "default")