Android SharedPreferences工具类

AndroidSharedPreferences大约 1393 字
public class SpUtil {
    private final SharedPreferences mSp;
    private final SharedPreferences.Editor mEditor;
    public SpUtil (Context context) {
        mSp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        mEditor = mSp.edit();
    }
     /*--------------- 读 ---------------*/
    /**
     * 读取字符信息
     * @param key
     * @param defValue
     * @return
     */
    public String getString(String key, String defValue) {
        return mSp.getString(key, defValue);
    }
    /**
     * 读取整型
     */
    public int getInt(String key, int defValue) {
        return mSp.getInt(key, defValue);
    }
    /**
     * 读取布尔型
     */
    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }
    /*--------------- 写 ---------------*/
    /**
     * 写入string
     * @param key
     * @param value
     */
    public void putString(String key, String value) {
        mEditor.putString(key, value);
        mEditor.commit();
    }
    /**
     * 写入int
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }
    /**
     * 写入boolean
     * @param key
     * @param value
     */
    public void putBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }
}
阅读 646 · 发布于 2019-04-12

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批