SharedPreferences的工具类

简介: import android.content.Context; import android.content.SharedPreferences; import android.content.
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);
        }

}

 

相关文章
|
6天前
|
JSON 网络协议 C#
C# 工具类
C# 工具类
18 1
|
7天前
|
Java
JavaMap工具类(MapUtils)
JavaMap工具类(MapUtils)
SharedPreferences封装
SharedPreferences封装 存 取 封装类
222 0
|
存储
1-SII--SharedPreferences完美封装
零、前言 以前我的SharedPreferences封装类根据不同类型getXXX,setXXX分好多情况。现在回过头看看,咱也是玩过泛型的。 突然灵光一闪,"少年,看你骨骼惊奇,泛型了解一下吗。
1053 0
|
存储 安全 Java
工具类
工具类
122 0
|
存储 XML 安全
SharedPreferences VS MMKV
简单了解 Android 轻量级存储 SharedPreferences 和 MMKV!
779 0
SharedPreferences VS MMKV
|
存储 XML 数据格式
SharedPreferences源码解析
终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~ 1.简介 写这篇博客目的在于巩固自己对SharedPreferences的理解。
1172 0
|
XML 数据格式 Java
全面剖析SharedPreferences
1.原理和概述 1.储存于硬盘上的xml键值对。 2.轻量级数据储存,数据多了容易引起性能问题 3.xml文件所在目录位于/data/data//shared_prefs/,可以有多个文件。
962 0