工具類之SPUtils

此工具類不是網(wǎng)上大家用爛的那一份,是博主親自編寫(xiě),親自測(cè)試,代碼簡(jiǎn)潔清晰,可滿足日常開(kāi)發(fā),不再是采用效率低下的commit來(lái)提交,而是apply,至于兩者有何不同,大家可以自行search。
Talk is cheap, show me ur code.下面上方法列表和具體代碼,開(kāi)車嘍,嗚~~

getInstance: 獲取SP實(shí)例
put        : SP中寫(xiě)入數(shù)據(jù)
getString  : SP中讀取String
getInt     : SP中讀取int
getLong    : SP中讀取long
getFloat   : SP中讀取float
getBoolean : SP中讀取boolean
getAll     : SP中獲取所有鍵值對(duì)
remove     : SP中移除該key
contains   : SP中是否存在該key
clear      : SP中清除所有數(shù)據(jù)

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/08/02
 *     desc  : SP相關(guān)工具類
 * </pre>
 */
public class SPUtils {

    private static Map<String, SPUtils> sSPMap = new HashMap<>();
    private SharedPreferences sp;

    /**
     * 獲取SP實(shí)例
     *
     * @return {@link SPUtils}
     */
    public static SPUtils getInstance() {
        return getInstance("");
    }

    /**
     * 獲取SP實(shí)例
     *
     * @param spName sp名
     * @return {@link SPUtils}
     */
    public static SPUtils getInstance(String spName) {
        if (isSpace(spName)) spName = "spUtils";
        SPUtils sp = sSPMap.get(spName);
        if (sp == null) {
            sp = new SPUtils(spName);
            sSPMap.put(spName, sp);
        }
        return sp;
    }

    private SPUtils(String spName) {
        sp = Utils.getContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    /**
     * SP中寫(xiě)入String
     *
     * @param key   鍵
     * @param value 值
     */
    public void put(@NonNull String key, @NonNull String value) {
        sp.edit().putString(key, value).apply();
    }

    /**
     * SP中讀取String
     *
     * @param key 鍵
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code null}
     */
    public String getString(@NonNull String key) {
        return getString(key, "");
    }

    /**
     * SP中讀取String
     *
     * @param key          鍵
     * @param defaultValue 默認(rèn)值
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code defaultValue}
     */
    public String getString(@NonNull String key, @NonNull String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    /**
     * SP中寫(xiě)入int
     *
     * @param key   鍵
     * @param value 值
     */
    public void put(@NonNull String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    /**
     * SP中讀取int
     *
     * @param key 鍵
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值-1
     */
    public int getInt(@NonNull String key) {
        return getInt(key, -1);
    }

    /**
     * SP中讀取int
     *
     * @param key          鍵
     * @param defaultValue 默認(rèn)值
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code defaultValue}
     */
    public int getInt(@NonNull String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    /**
     * SP中寫(xiě)入long
     *
     * @param key   鍵
     * @param value 值
     */
    public void put(@NonNull String key, long value) {
        sp.edit().putLong(key, value).apply();
    }

    /**
     * SP中讀取long
     *
     * @param key 鍵
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值-1
     */
    public long getLong(@NonNull String key) {
        return getLong(key, -1L);
    }

    /**
     * SP中讀取long
     *
     * @param key          鍵
     * @param defaultValue 默認(rèn)值
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code defaultValue}
     */
    public long getLong(@NonNull String key, long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    /**
     * SP中寫(xiě)入float
     *
     * @param key   鍵
     * @param value 值
     */
    public void put(@NonNull String key, float value) {
        sp.edit().putFloat(key, value).apply();
    }

    /**
     * SP中讀取float
     *
     * @param key 鍵
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值-1
     */
    public float getFloat(@NonNull String key) {
        return getFloat(key, -1f);
    }

    /**
     * SP中讀取float
     *
     * @param key          鍵
     * @param defaultValue 默認(rèn)值
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code defaultValue}
     */
    public float getFloat(@NonNull String key, float defaultValue) {
        return sp.getFloat(key, defaultValue);
    }

    /**
     * SP中寫(xiě)入boolean
     *
     * @param key   鍵
     * @param value 值
     */
    public void put(@NonNull String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }

    /**
     * SP中讀取boolean
     *
     * @param key 鍵
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code false}
     */
    public boolean getBoolean(@NonNull String key) {
        return getBoolean(key, false);
    }

    /**
     * SP中讀取boolean
     *
     * @param key          鍵
     * @param defaultValue 默認(rèn)值
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code defaultValue}
     */
    public boolean getBoolean(@NonNull String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * SP中寫(xiě)入String集合
     *
     * @param key    鍵
     * @param values 值
     */
    public void put(@NonNull String key, @NonNull Set<String> values) {
        sp.edit().putStringSet(key, values).apply();
    }

    /**
     * SP中讀取StringSet
     *
     * @param key 鍵
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code null}
     */
    public Set<String> getStringSet(@NonNull String key) {
        return getStringSet(key, Collections.<String>emptySet());
    }

    /**
     * SP中讀取StringSet
     *
     * @param key          鍵
     * @param defaultValue 默認(rèn)值
     * @return 存在返回對(duì)應(yīng)值,不存在返回默認(rèn)值{@code defaultValue}
     */
    public Set<String> getStringSet(@NonNull String key, @NonNull Set<String> defaultValue) {
        return sp.getStringSet(key, defaultValue);
    }

    /**
     * SP中獲取所有鍵值對(duì)
     *
     * @return Map對(duì)象
     */
    public Map<String, ?> getAll() {
        return sp.getAll();
    }

    /**
     * SP中移除該key
     *
     * @param key 鍵
     */
    public void remove(@NonNull String key) {
        sp.edit().remove(key).apply();
    }

    /**
     * SP中是否存在該key
     *
     * @param key 鍵
     * @return {@code true}: 存在<br>{@code false}: 不存在
     */
    public boolean contains(@NonNull String key) {
        return sp.contains(key);
    }

    /**
     * SP中清除所有數(shù)據(jù)
     */
    public void clear() {
        sp.edit().clear().apply();
    }

    private static boolean isSpace(String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}

如果該工具類依賴其他工具類,都可以在我的Android開(kāi)發(fā)人員不得不收集的代碼(持續(xù)更新中)中找到。

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

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

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