封装 SharedPreferences

本文提供了一套针对SharedPreferences的基本操作方法,包括保存与读取Int、String及Boolean类型数据的具体实现。适用于Android开发者快速掌握应用中轻量级数据存储的技术。
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * @ClassName: SharedUtil
 * @Description: 保存到 SharedPreferences 的数据
 * @author YongChen.Yu
 * @date 2016年10月31日
 *
 */
public class SharedUtil {

    static String SHARE_NAME = "name";

    public static SharedPreferences getDefaultSharedPreferences(Context context) {
        return context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
    }

    /**
     * @Title: putInt
     * @Description: 保存Int到本地
     * @param @param context
     * @param @param key
     * @param @param value 参数
     * @return void 返回类型
     * @throws
     */
    public static void putInt(Context context, String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    /**
     * @Title: getInt
     * @Description: 获取Int
     * @param @param context
     * @param @param key
     * @param @return 参数
     * @return int 返回类型
     * @throws
     */
    public static int getInt(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, 0);
    }

    /**
     * @Title: putString
     * @Description: 保存String到本地
     * @param @param context
     * @param @param key
     * @param @param value 参数
     * @return void 返回类型
     * @throws
     */
    public static void putString(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putString(key, value);
        edit.commit();
    }

    /**
     * @Title: getString
     * @Description: 获取String
     * @param @param context
     * @param @param key
     * @param @return 参数
     * @return String 返回类型
     * @throws
     */
    public static String getString(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, null);
    }

    /**
     * @Title: putBoolean
     * @Description: 保存 boolean 到本地
     * @param @param context
     * @param @param key
     * @param @param value 参数
     * @return void 返回类型
     * @throws
     */
    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * @Title: getBoolean
     * @Description:获取 boolean
     * @param @param context
     * @param @param key
     * @param @param defaultValue
     * @param @return 参数
     * @return boolean 返回类型
     * @throws
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defaultValue);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉辰56

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值