鍍金池/ 問(wèn)答/Java  Android/ SharedPreferences 無(wú)法保存數(shù)據(jù)

SharedPreferences 無(wú)法保存數(shù)據(jù)

代碼:

public class SPUtil {
    private static SharedPreferences sp;
    private static final String SP_TAG_NAME = "mySharedTag";
    public static void putString(Context ctx, String key,String value) {
        if(keyIsEmpty(key)) {
            return ;
        }
        if(sp == null) {
            sp = ctx.getSharedPreferences(SP_TAG_NAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }
}

問(wèn)題描述
當(dāng)我把手機(jī)關(guān)機(jī),重啟的時(shí)候,SP中的數(shù)據(jù)全部讀不到。。 這是什么情況。
注:SP中我保存了較多信息

回答
編輯回答
命多硬

你這只是寫(xiě)數(shù)據(jù)的SP方法,你怎么讀的SP數(shù)據(jù)

2018年9月18日 19:57
編輯回答
雨萌萌

這是我經(jīng)常用的一個(gè)SP工具類(lèi),直接存取就OK,你可以看看這個(gè)怎么寫(xiě)的

/**
 * SharedPreferences存儲(chǔ) 2018/01/19
 */

public class PreferenceUtils {

    private static SharedPreferences mSp;

    private static SharedPreferences getSp(Context context) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("settingInfo", Context.MODE_PRIVATE);
        }
        return mSp;
    }

    /**
     * 獲得boolean數(shù)據(jù),沒(méi)有時(shí)為false,方法重載
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * boolean數(shù)值,方法重載
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getBoolean(key, defValue);
    }

    /**
     * 設(shè)置boolean值
     */
    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 獲得String的數(shù)據(jù),沒(méi)有時(shí)為null
     */
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    /**
     * 獲得String的數(shù)據(jù),方法重載
     */
    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getString(key, defValue);
    }

    /**
     * 設(shè)置String值
     */
    public static void putString(Context context, String key, String value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 獲取int數(shù)據(jù),重載
     */
    public static int getInt(Context context, String key) {
        return getInt(context, key, 1);
    }

    /**
     * 獲取int數(shù)據(jù),重載
     */
    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getInt(key, defValue);
    }

    /**
     * 設(shè)置int數(shù)據(jù),重載
     */
    public static void putInt(Context context, String key, int value) {
        SharedPreferences sp = getSp(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }
}
2017年10月17日 07:02