Android SharedPreferences 工具类

Android About 1,398 words

工具类

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();
    }
}
Views: 3,979 · Posted: 2019-04-12

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

扫描下方二维码关注公众号和小程序↓↓↓
Today On History
Browsing Refresh