Android开发---使用SharedPreferences保存数据

使用SharedPreferences保存数据

SharedPreferences是Android中的一种轻量级的保存数据方式,同时使用起来简单方便,所以总结一下来供大家使用,同时下面也附带了一个使用的demo
废话不多说了,直接上代码~~~!!!





主要功能代码块



/**
 * @ClassName: CustomizeSharedPreferences
 * @Description: 使用SharedPreferences保存一些数据
 * @date 2016-3-7 下午5:23:47
 */
public class CustomizeSharedPreferences {
    /***
     * 定义一个保存缓存文件的名称
     */
    private static String config = "CONFIG";

    private static SharedPreferences preferences;

    /**
     * @Title: initPreferences
     * @Description:初始化SharedPreferences
     * @param context
     *            上下文
     * @return void
     * @throws
     */
    private static void initPreferences(Context context) {
        if (null == preferences) {
            preferences = context.getSharedPreferences(config,
                    Context.MODE_PRIVATE);
        }
    }

    /**
     * @Title: setIntData
     * @Description: 设置Int类型值
     * @param context
     *            上下文
     * @param key
     * @param value
     * @return void
     * @throws
     */
    public static void setIntData(Context context, String key, int value) {
        initPreferences(context);
        preferences.edit().putInt(key, value).commit();
    }

    /**
     * @Title: getIntData
     * @Description: 得到int类型的返回值
     * @param context
     * @param key
     * @return int 默认值返回-1
     * @throws
     */
    public static int getIntData(Context context, String key) {
        initPreferences(context);
        return preferences.getInt(key, -1);
    }

    /**
     * 
     * @Title: setStringData
     * @Description: 设置String类型的值
     * @param context
     * @param key
     * @param value
     * @return void
     * @throws
     */
    public static void setStringData(Context context, String key, String value) {
        initPreferences(context);
        preferences.edit().putString(key, value).commit();
    }

    /**
     * @Title: getStringData
     * @Description: 得到String类型的值
     * @param context
     * @param key
     * @return String 默认返回值是“”,空
     * @throws
     */
    public static String getStringData(Context context, String key) {
        initPreferences(context);
        return preferences.getString(key, "");
    }

    /**
     * @Title: setBooleanData
     * @Description: 设置布尔型的值
     * @param context
     * @param key
     * @param value
     * @return void
     * @throws
     */
    public static void setBooleanData(Context context, String key, boolean value) {
        initPreferences(context);
        preferences.edit().putBoolean(key, value).commit();
    }

    /**
     * @Title: getBooleanData
     * @Description: 得到布尔型的值
     * @param context
     * @param key
     * @return boolean 默认返回false
     * @throws
     */
    public static boolean getBooleanData(Context context, String key) {
        initPreferences(context);
        return preferences.getBoolean(key, false);
    }

    /**
     * @Title: clearData
     * @Description: 清除 SharedPreferences保存的所有数据
     * @return void
     * @throws
     */
    public static void clearData() {
        preferences.edit().clear().commit();
    }
}

↓↓↓↓↓↓↓↓

点我下载

  • **欢迎浏览~~
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值