android 共享首先项SharedPreferences



package com.test.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 共享首先项<br/>
 * 通常保存在/data/data/package_name/shared_prefs/目录下,文件名为变量MY_PROJECT_NAME对应的值
 * 
 * @ClassName: SharedPreferenceUtil
 * @Description: 共享首先项辅助类,可以设置或获取相应的字符串、整型、长整型、浮点数、布尔值
 * @author linchunda
 * @date 2014年6月17日 下午3:49:52
 * 
 */
public class SharedPreferenceUtil {
	/** 共享首先项文件名,一般采用项目名称 */
	private final static String MY_PROJECT_NAME = "bjt_preference";

	/**
	 * 保存字符串
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            字符串的键
	 * @param value
	 *            对应的值
	 */
	public static void setString(Context context, String key, String value) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 保存数值
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @param value
	 *            对应的值
	 */
	public static void setInt(Context context, String key, int value) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	/**
	 * 保存数值
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @param value
	 *            对应的值
	 */
	public static void setLong(Context context, String key, long value) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/**
	 * 保存数值
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @param value
	 *            对应的值
	 */
	public static void setFloat(Context context, String key, float value) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putFloat(key, value);
		editor.commit();
	}

	/**
	 * 保存布尔值
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @param value
	 *            对应的值
	 */
	public static void setBoolean(Context context, String key, boolean value) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 获取字符串的值
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @return 返回健key对应的值
	 */
	public static String getString(Context context, String key) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		return preferences.getString(key, null);
	}

	/**
	 * 获取数值,如果不存在相应的键,返回0
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @return 返回健key对应的值
	 */
	public static int getInt(Context context, String key) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		return preferences.getInt(key, 0);
	}

	/**
	 * 获取数值,如果不存在相应的键,返回0
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @return 返回健key对应的值
	 */
	public static long getLong(Context context, String key) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		return preferences.getLong(key, 0);
	}

	/**
	 * 获取数值,如果不存在相应的键,返回0
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @return 返回健key对应的值
	 */
	public static float getFloat(Context context, String key) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		return preferences.getFloat(key, 0);
	}

	/**
	 * 获取布尔值,如果不存在相应的键,返回false
	 * 
	 * @param context
	 *            内容上下文
	 * @param key
	 *            对应的键
	 * @return 返回健key对应的值
	 */
	public static boolean getBoolean(Context context, String key) {
		SharedPreferences preferences = context.getSharedPreferences(
				MY_PROJECT_NAME, Context.MODE_PRIVATE);
		return preferences.getBoolean(key, false);
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值