SharedPreferences的封装SharedPreferenceUtils

本文介绍了如何对SharedPreferences和SharedPreferences.Editor进行简单封装,提供方便的数据保存、获取、删除及检查方法,包括put系列、get系列、remove、clear及contains等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 对SharedPreferences以及SharedPreferences.Editor进行了简单的封装,尽可能的接近两者“原生”的操作方式。

数据的保存封装了:putInt,putFloat,putLong,putBoolean,putString,putStringSet

数据的获取封装了:getInt,getFloat,getLong,getBoolean,getString,getStringSet,getAll

数据的删除封装了:remove,clear

数据的检查封装了:contains


import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;


public class SharedPreferenceUtils {
	
	private static String sharedPreferencesName = "";

	/**
	 * 获取默认偏好设置编辑器Editor
	 * @param context
	 * @return
	 */
	private static Editor getEditor(Context context) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		Editor editor = sharedPreferences.edit();
		return editor;
	}
	
	/**
	 * 获取偏好设置SharedPreferences
	 * @param context
	 * @return
	 */
	private static SharedPreferences getSharedPreferences(Context context){
		SharedPreferences sharedPreferences;
		if(sharedPreferencesName.isEmpty()){
			//获取默认的sharedPreferences
			sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		}else{
			sharedPreferences = context.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
		}
		
		return sharedPreferences;
		
	}
	
	/**
	 * 存储偏好设置键值对
	 * @param context
	 * @param key
	 * @param value 默认值
	 */
	public static void putInt(Context context, String key, Integer value){
		Editor editor = getEditor(context);
		editor.putInt(key, value);
		editor.commit();
	}
	
	/**
	 * 存储偏好设置键值对
	 * @param context
	 * @param key
	 * @param value 默认值
	 */
	public static void putFloat(Context context, String key, Float value){
		Editor editor = getEditor(context);
		editor.putFloat(key, value);
		editor.commit();
	}
	
	/**
	 * 存储偏好设置键值对
	 * @param context
	 * @param key
	 * @param value 默认值
	 */
	public static void putLong(Context context, String key, Long value){
		Editor editor = getEditor(context);
		editor.putLong(key, value);
		editor.commit();
	}
	
	/**
	 * 存储偏好设置键值对
	 * @param context
	 * @param key
	 * @param value 默认值
	 */
	public static void putBoolean(Context context, String key, Boolean value){
		Editor editor = getEditor(context);
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	/**
	 * 存储偏好设置键值对
	 * @param context
	 * @param key
	 * @param value 默认值
	 */
	public static void putString(Context context, String key, String value){
		Editor editor = getEditor(context);
		editor.putString(key, value);
		editor.commit();
	}
	
	/**
	 * 存储偏好设置键值对集合
	 * @param context
	 * @param key
	 * @param values
	 */
	public static void putStringSet(Context context, String key, Set<String> values){
		Editor editor = getEditor(context);
		editor.putStringSet(key, values);
		editor.commit();
	}
	
	

	
	/**
	 * 获取指定偏好设置
	 * @param context
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Integer getInt(Context context, String key,Integer defaultValue){
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.getInt(key, defaultValue);
	}
	
	/**
	 * 获取指定偏好设置
	 * @param context
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Float getFloat(Context context, String key,Float defaultValue){
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.getFloat(key, defaultValue);
	}
	
	/**
	 * 获取指定偏好设置
	 * @param context
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Long getLong(Context context, String key,Long defaultValue){
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.getLong(key, defaultValue);
	}
	
	/**
	 * 获取指定偏好设置
	 * @param context
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Boolean getBoolean(Context context, String key,Boolean defaultValue){
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.getBoolean(key, defaultValue);
	}
	
	/**
	 * 获取指定偏好设置
	 * @param context
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getString(Context context, String key,String defaultValue){
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.getString(key, defaultValue);
	}
	
	/**
	 * 获取指定偏好设置
	 * @param context
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Set<String> getStringSet(Context context, String key,Set<String> defaultValue){
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.getStringSet(key, defaultValue);
	}
	
	/**
	 * 获取所有偏好设置
	 * @param context
	 * @return
	 */
	public static Map<String, ?> getAll(Context context) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		Map<String, ?> all =  sharedPreferences.getAll();
		return all;
	}
	
	/**
	 * 删除指定偏好设置
	 * @param context
	 * @param key
	 */
	public static void remove(Context context, String key) {
		Editor editor = getEditor(context);
		editor.remove(key);
		editor.commit();
	}
	
	/**
	 * 清空所有偏好设置
	 * @param context
	 */
	public static void clear(Context context){
		Editor editor = getEditor(context);
		editor.clear();
		editor.commit();
	}
	
	/**
	 * 判断键名对应值是否存在
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean contains(Context context, String key) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		return sharedPreferences.contains(key);
	}
	
	

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值