Android下SharedPreferences的封装SharedPreferenceUtils

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/water_3700348/article/details/85156293

 对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);
	}
	
	

}

猜你喜欢

转载自blog.csdn.net/water_3700348/article/details/85156293
今日推荐