在平时写代码的过程中,可能会用到使用SharedPreferences来保存数据,用一次写一次保存的过程,代码重复率太多,于是封装起来:
以下是封装的工具类和使用方法:
封装的工具类:
public class SPUtils { private SharedPreferences sp; private SharedPreferences.Editor editor; /** * SPUtils构造函数 * <p>在Application中初始化</p> * * @param spName spName */ public SPUtils(String spName) { sp = CommonUtils.getInstance().getContext().getSharedPreferences(spName, Context.MODE_PRIVATE); editor = sp.edit(); editor.apply(); } /** * SP中写入String类型value * * @param key 键 * @param value 值 */ public void putString(String key, String value) { editor.putString(key, value).apply(); } /** * SP中读取String * * @param key 键 * @return 存在返回对应值,不存在返回默认值{@code null} */ public String getString(String key) { return getString(key, null); } /** * SP中读取String * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public String getString(String key, String defaultValue) { return sp.getString(key, defaultValue); } /** * SP中写入int类型value * * @param key 键 * @param value 值 */ public void putInt(String key, int value) { editor.putInt(key, value).apply(); } /** * SP中读取int * * @param key 键 * @return 存在返回对应值,不存在返回默认值-1 */ public int getInt(String key) { return getInt(key, -1); } /** * SP中读取int * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public int getInt(String key, int defaultValue) { return sp.getInt(key, defaultValue); } /** * SP中写入long类型value * * @param key 键 * @param value 值 */ public void putLong(String key, long value) { editor.putLong(key, value).apply(); } /** * SP中读取long * * @param key 键 * @return 存在返回对应值,不存在返回默认值-1 */ public long getLong(String key) { return getLong(key, -1L); } /** * SP中读取long * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public long getLong(String key, long defaultValue) { return sp.getLong(key, defaultValue); } /** * SP中写入float类型value * * @param key 键 * @param value 值 */ public void putFloat(String key, float value) { editor.putFloat(key, value).apply(); } /** * SP中读取float * * @param key 键 * @return 存在返回对应值,不存在返回默认值-1 */ public float getFloat(String key) { return getFloat(key, -1f); } /** * SP中读取float * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public float getFloat(String key, float defaultValue) { return sp.getFloat(key, defaultValue); } /** * SP中写入boolean类型value * * @param key 键 * @param value 值 */ public void putBoolean(String key, boolean value) { editor.putBoolean(key, value).apply(); } /** * SP中读取boolean * * @param key 键 * @return 存在返回对应值,不存在返回默认值{@code false} */ public boolean getBoolean(String key) { return getBoolean(key, false); } /** * SP中读取boolean * * @param key 键 * @param defaultValue 默认值 * @return 存在返回对应值,不存在返回默认值{@code defaultValue} */ public boolean getBoolean(String key, boolean defaultValue) { return sp.getBoolean(key, defaultValue); } /** * SP中获取所有键值对 * * @return Map对象 */ public Map<String, ?> getAll() { return sp.getAll(); } /** * SP中移除该key * * @param key 键 */ public void remove(String key) { editor.remove(key).apply(); } /** * SP中是否存在该key * * @param key 键 * @return {@code true}: 存在<br>{@code false}: 不存在 */ public boolean contains(String key) { return sp.contains(key); } /** * SP中清除所有数据 */ public void clear() { editor.clear().apply(); } }
使用方法:
new SPUtils(GlobalFileName.APP_CONFIG).putBoolean("isFirst",false);