Android 之SharedPreferences存储

封装好的一个工具类、直接上代码:

/**
 * @author Jenly
 * @date 2014-8-8
 */
public class SharedPreferencesUtils {
	
	public static final String PREF_NAME = "org.king.pref_name_jenly";
	
	public static SharedPreferences getSharedPreferences(Context context){
		return getSharedPreferences(context, PREF_NAME);
	}
	
	public static SharedPreferences getSharedPreferences(Context context, String prefName){
		return context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
	}
	
	//--------------------------Int
	public static void putInt(Context context,String key,int value){
		getSharedPreferences(context).edit().putInt(key, value).commit();
	}
	
	public static int getInt(Context context,String key,int defValue){
		return getSharedPreferences(context).getInt(key, defValue);
	}
	
	public static int getInt(Context context,String key){
		return getInt(context,key,0);
	}
	
	//--------------------------Float
	public static void putFloat(Context context,String key,float value){
		getSharedPreferences(context).edit().putFloat(key, value).commit();
	}
	
	public static float getFloat(Context context,String key,float defValue){
		return getSharedPreferences(context).getFloat(key, defValue);
	}
	public static float getFloat(Context context,String key){
		return getFloat(context, key, 0);
	}
	
	//--------------------------Long
	public static void putLong(Context context,String key,long value){
		getSharedPreferences(context).edit().putLong(key, value).commit();
	}
	
	public static long getLong(Context context,String key,long defValue){
		return getSharedPreferences(context).getLong(key, defValue);
	}
	public static long getLong(Context context,String key){
		return getLong(context, key, 0);
	}
	
	//--------------------------Boolean
	public static void putBoolean(Context context,String key,boolean value){
		getSharedPreferences(context).edit().putBoolean(key, value).commit();
	}
	
	public static boolean getBoolean(Context context,String key,boolean defValue){
		return getSharedPreferences(context).getBoolean(key, defValue);
	}
	public static boolean getBoolean(Context context,String key){
		return getBoolean(context, key, false);
	}
	
	//--------------------------String
	public static void putString(Context context,String key,String value){
		getSharedPreferences(context).edit().putString(key, value).commit();
	}
	
	public static String getString(Context context,String key,String defValue){
		return getSharedPreferences(context).getString(key, defValue);
	}
	
	public static String getString(Context context,String key){
		return getString(context, key, null);
	}
	
	//--------------------------Map
	@SuppressLint("CommitPrefEdits")
	public static void putMapString(Context context,Map<String,String> map){
		if(map!=null){
			SharedPreferences.Editor editor = getSharedPreferences(context).edit();
			
			for(Entry<String, String> entry: map.entrySet()){
				if(!TextUtils.isEmpty(entry.getKey()))
					editor.putString(entry.getKey(), entry.getValue());
			}
			
			editor.commit();
			
		}
	}
	
	//--------------------------Increase int
	/**
	 * 自增(默认自增1)
	 * @param context
	 * @param key
	 */
	public static void increase(Context context,String key){
		increase(context, key,1);
	}
	
	/**
	 * 自增
	 * @param context
	 * @param key
	 * @param deltaValue 增量值(基数)
	 */
	public static void increase(Context context,String key,int deltaValue){
		getSharedPreferences(context).edit().putInt(key,getInt(context, key)+deltaValue).commit();
	}
	
}


转载于:https://www.cnblogs.com/jenly/p/5312365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值