SharedPreferences工具类封装

SharedPreferences主要是用来保存用户的一些配置信息,基本在每个App中都会用到,今天简单的进行了封装以便在以后的项目中能够复用!代码很简单直接上代码。

public class SharedPreferencesUtil {
    private static final String SHARED_PATH = "profile";
    private static SharedPreferencesUtil instance;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Context mContext;


    public static SharedPreferencesUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (SharedPreferencesUtil.class) {
                if (instance == null) {
                    instance = new SharedPreferencesUtil(context);
                }
            }
        }
        return instance;
    }

    public SharedPreferencesUtil(Context mContext) {
        this.mContext = mContext;
        sharedPreferences = mContext.getSharedPreferences(SHARED_PATH, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public void putString(String key, String value) {
        if (key != null && !key.equals("")) {
            editor.putString(key, value);
            editor.commit();
        }
    }

    public void putInt(String key, int value) {
        if (key != null && !key.equals("")) {
            editor.putInt(key, value);
            editor.commit();
        }
    }

    public void putBoolean(String key, boolean value) {
        if (key != null && !key.equals("")) {
            editor.putBoolean(key, value);
            editor.commit();
        }
    }

    public void putLong(String key, Long value) {
        if (key != null && !key.equals("")) {
            editor.putLong(key, value);
            editor.commit();
        }
    }

    public void putFloat(String key, float value) {
        if (key != null && !key.equals("")) {
            editor.putFloat(key, value);
            editor.commit();
        }
    }

    public String getString(String key) {
        if (key != null && !key.equals("")) {
            return sharedPreferences.getString(key, "");
        }
        return null;
    }

    public int getInt(String key) {
        if (key != null && !key.equals("")) {
            return sharedPreferences.getInt(key, 0);
        }
        return 0;
    }

    public float getFloat(String key) {
        if (key != null && !key.equals("")) {
            return sharedPreferences.getFloat(key, 0);
        }
        return 0;
    }

    public boolean getBoolean(String key) {
        if (key != null && !key.equals("")) {
            return sharedPreferences.getBoolean(key, false);
        }
        return false;
    }

    public long getLong(String key) {
        if (key != null && !key.equals("")) {
            return sharedPreferences.getLong(key, 0);
        }
        return 0;
    }

    public boolean contains(String key) {
        return sharedPreferences.contains(key);
    }

    public void remove(String key) {
        editor.remove(key);
        editor.commit();
    }

    public void clear() {
        editor.clear();
        editor.commit();
    }
}

这样SharedPreferencesUtil的封装就完成了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值