SharedPreferences的工具类

本文介绍了一个针对Android平台的SharedPreferences工具类实现,该工具类提供了多种数据类型存储与读取的方法,方便开发者进行应用配置信息的持久化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/**
* SharedPreferences的工具类
* @author wangfubin
*/
public class Sp {

        private static String name = "config";

        /**
         * 获取SharedPreferences实例对象
         *
         * @param context
         * @return
         */
        public static SharedPreferences getSharedPreference(Context context) {
                return context.getSharedPreferences(name, Context.MODE_PRIVATE);
        }

        /**
         * 保存一个Boolean类型的值!
         * @param context
         * @param key
         * @param value
         * @return
         */
        public static boolean putBoolean(Context context, String key, Boolean value) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                Editor editor = sharedPreference.edit();
                editor.putBoolean(key, value);
                return editor.commit();
        }
        /**
         * 保存一个int类型的值!
         * @param context
         * @param key
         * @param value
         * @return
         */
        public static boolean putInt(Context context, String key, int value) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                Editor editor = sharedPreference.edit();
                editor.putInt(key, value);
                return editor.commit();
        }
        /**
         * 保存一个float类型的值!
         * @param context
         * @param key
         * @param value
         * @return
         */
        public static boolean putFloat(Context context, String key, float value) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                Editor editor = sharedPreference.edit();
                editor.putFloat(key, value);
                return editor.commit();
        }
        /**
         * 保存一个long类型的值!
         * @param context
         * @param key
         * @param value
         * @return
         */
        public static boolean putLong(Context context, String key, long value) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                Editor editor = sharedPreference.edit();
                editor.putLong(key, value);
                return editor.commit();
        }
        /**
         * 保存一个String类型的值!
         * @param context
         * @param key
         * @param value
         * @return
         */
        public static boolean putString(Context context, String key, String value) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                Editor editor = sharedPreference.edit();
                editor.putString(key, value);
                return editor.commit();
        }

        /**
         * 获取String的value
         *
         * @param context
         * @param key
         * 名字
         * @param defValue
         * 默认值
         * @return
         */
        public static String getString(Context context, String key, String defValue) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                return sharedPreference.getString(key, defValue);
        }

        /**
         * 获取int的value
         *
         * @param context
         * @param key
         * 名字
         * @param defValue
         * 默认值
         * @return
         */
        public static int getInt(Context context, String key, int defValue) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                return sharedPreference.getInt(key, defValue);
        }

        /**
         * 获取float的value
         *
         * @param context
         * @param key
         * 名字
         * @param defValue
         * 默认值
         * @return
         */
        public static float getFloat(Context context, String key, Float defValue) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                return sharedPreference.getFloat(key, defValue);
        }

        /**
         * 获取boolean的value
         *
         * @param context
         * @param key
         * 名字
         * @param defValue
         * 默认值
         * @return
         */
        public static boolean getBoolean(Context context, String key,
                        Boolean defValue) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                return sharedPreference.getBoolean(key, defValue);
        }

        /**
         * 获取long的value
         *
         * @param context
         * @param key
         * 名字
         * @param defValue
         * 默认值
         * @return
         */
        public static long getLong(Context context, String key, long defValue) {
                SharedPreferences sharedPreference = getSharedPreference(context);
                return sharedPreference.getLong(key, defValue);
        }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值