安卓开发封装SharedPreferences类

安卓开发中会常常使用到SharedPreferences存储方式来存储一些控件的状态或者是密码信息,因此我们就需要

对SharedPreferences类进行封装,一劳永逸,也可以简化代码,使代码变得简洁。

工具类的封装-SharedPreferences封装

下面是代码

package com.gougoucompany.clarence.smartbutler.utils;

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

/**
 * 项目名:   SmartButler
 * 包名:     com.gougoucompany.clarence.smartbutler.utils
 * 文件名:   ShareUtils
 * 创建时间: 2018/4/28 8:54
 * 英文名:   Clarence
 * 中文名:   习伟博
 * 描述:
 * SharedPreference封装:
 * 1.定义存取方式get/put
 * 2.明确数据类型Int/String/Boolean
 * 3.定义删除功能 单个/全部

 */

public class ShareUtils {

    public static final String NAME = "config";

    //键 值
    public static void putInt(Context mContext, String key, int value){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().putInt(key ,value).commit();
    }

    public static void putString(Context mContext, String key, String value){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().putString(key ,value).commit();
    }

    public static void putBoolean(Context mContext, String key, boolean value){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().putBoolean(key ,value).commit();
    }

    //键 默认值
    public static String getString(Context mContext, String key, String defValue){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    public static int getInt(Context mContext, String key, int defValue){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static boolean getBoolean(Context mContext, String key, boolean defValue){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }

    //删除单个
    public static void deleShare(Context mContext, String key){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().remove(key).commit();
    }

    //删除全部键值对信息
    public static void deleAll(Context mContext){
        SharedPreferences sp = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_32252957/article/details/80118688