SharedPreferences單例模式,支持多對象調用

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.hwariot.lib.tools.ToolsLibAPP;

import java.util.HashMap;

/***
 *@date 創(chuàng)建時間 2018/4/18 15:16
 *@author 作者: W.YuLong
 *@description SharedPreferences的單例模式,支持不同的命名
 */
public class SPSingleton {
    private static volatile HashMap<String, SPSingleton> instanceMap = new HashMap<>();

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    //是否是執(zhí)行apply的模式,false表示為commit保存數據
    private boolean isApplyMode = false;
    private static final String DEFAULT = "default";

    private SPSingleton(String name) {
        if (DEFAULT.equals(name)) {
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ToolsLibAPP.get());
        } else {
            sharedPreferences = ToolsLibAPP.get().getSharedPreferences(name, Context.MODE_PRIVATE);
        }
        editor = sharedPreferences.edit();
    }

    public static SPSingleton get(String name) {
        if (instanceMap.get(name) == null) {
            synchronized (SPSingleton.class) {
                if (instanceMap.get(name) == null) {
                    instanceMap.put(name, new SPSingleton(name));
                }
            }
        }
        //這里每次get操作時強制將保存模式改為commit的方式
        instanceMap.get(name).isApplyMode = false;
        return instanceMap.get(name);
    }

    public static SPSingleton get() {
        return get(DEFAULT);
    }

    // 如果用apply模式的話,得要先調用這個方法,
    // 然后鏈式調用后續(xù)的存儲方法,最后以commit方法結尾
    public SPSingleton applyMode() {
        isApplyMode = true;
        return this;
    }

    public void commit() {
        isApplyMode = false;
        editor.commit();
    }

    public SPSingleton putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        save();
        return this;
    }

    private void save() {
        if (isApplyMode) {
            editor.apply();
        } else {
            editor.commit();
        }
    }

    public SPSingleton putFloat(String key, float value) {
        editor.putFloat(key, value);
        save();
        return this;
    }

    public float getFloat(String key, float defValue){
        return sharedPreferences.getFloat(key, defValue);
    }

    public SPSingleton putLong(String key, long value) {
        editor.putLong(key, value);
        save();
        return this;
    }

    public long getLong(String key, long defValue){
        return sharedPreferences.getLong(key, defValue);
    }

    public SPSingleton putInt(String key, int value) {
        editor.putInt(key, value);
        save();
        return this;
    }

    public SPSingleton putString(String key, String value) {
        editor.putString(key, value);
        save();
        return this;
    }

    public String getString(String key, String defValue) {
        return sharedPreferences.getString(key, defValue);
    }

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

    public int getInt(String key, int defValue) {
        return sharedPreferences.getInt(key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sharedPreferences.getBoolean(key, defValue);
    }

}
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 這個模式是很有意思,而且比較簡單,但是我還是要說因為它使用的是如此的廣泛, 如此的有人緣,單例就是單一、獨苗的意思...
    迎風布陣x閱讀 608評論 0 0
  • 古詩云:“沉舟側畔千帆過,病樹前頭萬木春”新事物必將取代舊事物,所以如果故步自封,抱殘守缺必定會在巨大的社會競...
    1路有李閱讀 174評論 2 7
  • 文 | 桑桑姐 17年因為工作,要長途搬家,作為有娃一族,唯一剛需,當然是買學區(qū)房。 日光之下并無新事,美國和中國...
    _飛魚閱讀 1,471評論 9 53
  • 一路走來 聽說特別喜歡懷念過去的人總是容易感傷,因為存在在過去里的人和事總會在不經意間挑起你的某根神經,刺激你回憶...
    叼偽徐娘閱讀 134評論 0 0
  • 晚上吃了山東印象的羊肉湯這次超多羊肉啊……晚上普拉提很給力,基本動作很重要,教練說沒有做一個俯臥撐一個月想塑形不可...
    竹林風lf閱讀 181評論 0 0

友情鏈接更多精彩內容