package com.sets.speedtest.utils; import android.content.Context; import android.content.SharedPreferences; /** * SharedPreference:工具类 * * @author : lipan * @create_time : 2014年4月18日 下午5:15:09 * @desc : SharedPreference:工具类 * @update_time : * @update_desc : * */ public class SharedPreferencesUtils { private static final String NAME = "com.sets.speedtest"; private SharedPreferences sp; private SharedPreferences.Editor editor; public SharedPreferencesUtils(Context context) { sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE); } public void putString(String k, String v) { boolean autoCommit; if (editor == null) { editor = sp.edit(); autoCommit = true; } else { autoCommit = false; } editor.putString(k, v); if (autoCommit) { editor.commit(); editor = null; } } public String getString(String k, String def) { return sp.getString(k, def); } public void putInt(String k, int v) { boolean autoCommit; if (editor == null) { editor = sp.edit(); autoCommit = true; } else { autoCommit = false; } editor.putInt(k, v); if (autoCommit) { editor.commit(); editor = null; } } public int getInt(String k, int def) { return sp.getInt(k, def); } public void putLong(String k, long v) { boolean autoCommit; if (editor == null) { editor = sp.edit(); autoCommit = true; } else { autoCommit = false; } editor.putLong(k, v); if (autoCommit) { editor.commit(); editor = null; } } public long getLong(String k, long def) { return sp.getLong(k, def); } public void putFloat(String k, float v) { boolean autoCommit; if (editor == null) { editor = sp.edit(); autoCommit = true; } else { autoCommit = false; } editor.putFloat(k, v); if (autoCommit) { editor.commit(); editor = null; } } public float getFloat(String k, float def) { return sp.getFloat(k, def); } public void putBoolean(String k, boolean v) { boolean autoCommit; if (editor == null) { editor = sp.edit(); autoCommit = true; } else { autoCommit = false; } editor.putBoolean(k, v); if (autoCommit) { editor.commit(); editor = null; } } public boolean getBoolean(String k, boolean def) { return sp.getBoolean(k, def); } public void edit() { if (editor == null) editor = sp.edit(); } public void commit() { if (editor != null) { editor.commit(); editor = null; } } }