码迷,mamicode.com
首页 > 移动开发 > 详细

Android 之SharedPreferences存储

时间:2015-03-19 10:12:29      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:sharedpreferences   存储   工具类   共享存储   properties   

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

/**
 * @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();
	}
	
}


Android 之SharedPreferences存储

标签:sharedpreferences   存储   工具类   共享存储   properties   

原文地址:http://blog.csdn.net/jenly121/article/details/44452651

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!