PreferencesUtils工具類,簡化preference操作

public class PreferencesUtils {

    private Context context;

    public PreferencesUtils(Context context) {
        this.context = context;
    }

    public boolean putInt(String key, int value) {
        boolean flag = context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putInt(key, value).commit();
        return flag;
    }

    public int getInt(String key, int defValue) {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getInt(key, defValue);
    }

    public boolean putLong(String key, long value) {
        boolean flag = context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putLong(key, value).commit();
        return flag;
    }

    public long getLong(String key, long defValue) {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getLong(key, defValue);
    }

    public boolean putString(String key, String value) {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putString(key, value).commit();
    }

    public String getString(String key, String defValue) {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getString(key, defValue);
    }

    public boolean putBoolean(String key, boolean value) {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key, boolean defValue) {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).getBoolean(key, defValue);
    }

    public boolean clearAllValue() {
        return context.getSharedPreferences(Constant.PREFERENCE_NAME, Context.MODE_PRIVATE).edit().clear().commit();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容