Android 封装SharedPreference工具类

/**
 * @anthor GrainRain
 * @funcation SharedPreference工具类
 * @date 2019/9/5
 */
public class SharedPreferenceUtils {

    private static SharedPreferences preferences;
    private static SharedPreferences.Editor editor;
    private static final String TAG = "SharedPreference";

    /**
     * 储存数据
     * @param key
     * @param object
     */
    public static void put(Context context, String key, Object object) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();

        if(editor != null) {
            if (object instanceof String) {
                editor.putString(key, (String) object);
            } else if (object instanceof Integer) {
                editor.putInt(key, (Integer) object);
            } else if (object instanceof Boolean) {
                editor.putBoolean(key, (Boolean) object);
            } else if (object instanceof Float) {
                editor.putFloat(key, (Float) object);
            } else if (object instanceof Long) {
                editor.putLong(key, (Long) object);
            } else {
                editor.putString(key, object.toString());
            }
            editor.commit();
        } else {
            Log.e(TAG, "请重试");
        }
    }

    /**
     * 获取数据
     * @param key
     * @param defaultObject
     * @return Object
     */
    public static Object get(Context context, String key, Object defaultObject) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();

        if(preferences != null) {
            if (defaultObject instanceof String) {
                return preferences.getString(key, (String) defaultObject);
            } else if (defaultObject instanceof Integer) {
                return preferences.getInt(key, (Integer) defaultObject);
            } else if (defaultObject instanceof Boolean) {
                return preferences.getBoolean(key, (Boolean) defaultObject);
            } else if (defaultObject instanceof Float) {
                return preferences.getFloat(key, (Float) defaultObject);
            } else if (defaultObject instanceof Long) {
                return preferences.getLong(key, (Long) defaultObject);
            } else {
                return preferences.getString(key, null);
            }
        } else {
            Log.e(TAG, "请重试");
            return null;
        }
    }

    /**
     * 移除key对应的值
     * @param key
     */
    public static void remove(Context context, String key) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();

        if(editor != null) {
            editor.remove(key);
            editor.commit();
        } else {
            Log.e(TAG, "请重试");
        }
    }

    /**
     * 清除所有数据
     */
    public static void clear(Context context) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();

        if(editor != null) {
            editor.clear();
            editor.commit();
        } else {
            Log.e(TAG, "请重试");
        }
    }

    /**
     * 查询key是否存在
     */
    public static Boolean contain(Context context, String key) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();

        if(preferences != null) {
            return preferences.contains(key);
        } else {
            Log.e(TAG, "请重试");
            return false;
        }
    }

    /**
     * 返回所有键值对
     * @return
     */
    public static Map<String, ?> getAll(Context context) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = preferences.edit();

        if(preferences != null) {
            return preferences.getAll();
        } else {
            Log.e(TAG, "请重试");
            return null;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值