Android封装SharedPreferences工具类

本文介绍了一个针对SharedPreferences的基础操作工具类,包括数据的保存、读取、删除及缓存清理等功能。该工具类支持多种基本数据类型,并通过实例演示了如何在Android应用中使用这些功能。

工具类基本功能说明:
1、保存基本数据类型数据;
2、读取基本数据类型数据;
3、依据key删除对应value;
4、清除SharedPreferences缓存数据静态方法;

package helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

public class SharedPreferenceHelper {

    public static final String SHARED_PREFERENCES = "Study";

    /**
     * 保存基本数据类型的数据
     * @param context
     * @param key
     * @param value
     */
    @SuppressLint("CommitPrefEdits")
    public static void saveValue(Context context, String key, Object value) {

        SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES, 0).edit();

        if (!TextUtils.isEmpty(key) && value != null) {

            if (value instanceof String) {
                editor.putString(key, (String)value);

            } else if (value instanceof Integer) {
                editor.putInt(key, (Integer) value);

            } else if (value instanceof Long) {
                editor.putLong(key, (Long) value);

            } else if (value instanceof Float) {
                editor.putFloat(key, (Float) value);

            } else if (value instanceof Boolean) {
                editor.putBoolean(key, (Boolean)value);

            }
        }
    }

    /**
     * 读取基本数据类型的数据
     * @param context
     * @param key
     * @param classT
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getValue(Context context, String key, Class<T> classT) {

        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES, 0);

        if (!TextUtils.isEmpty(key) && classT != null) {
            String className = classT.getSimpleName();

            if ("String".equals(className)) {
                return (T) sharedPreferences.getString(key, null);

            } else if ("Integer".equals(className)) {
                return (T) Integer.valueOf(sharedPreferences.getInt(key, -1));

            } else if ("Long".equals(className)) {
                return (T) Long.valueOf(sharedPreferences.getLong(key, -1));

            } else if ("Float".equals(className)) {
                return (T) Float.valueOf(sharedPreferences.getFloat(key, -1));

            } else if ("Boolean".equals(className)) {
                return (T) Boolean.valueOf(sharedPreferences.getBoolean(key, false));

            }
        }

        return null;
    }

    /**
     * 删除值
     * @param context
     * @param key
     */
    @SuppressLint("CommitPrefEdits")
    public static void deleteValue(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES, 0);
        Editor editor = sharedPreferences.edit();
        editor.remove(key);
    }

    /**
     * 清除缓存数据
     * @param context
     */
    @SuppressLint("CommitPrefEdits")
    public static void clearData(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES, 0);
        Editor editor = sharedPreferences.edit();
        editor.clear();
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值