SharedPreferences 无法保存数据

代码:

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();
    }
}

问题描述
当我把手机关机,重启的时候,SP中的数据全部读不到。。 这是什么情况。
注:SP中我保存了较多信息

阅读 3.6k
2 个回答

这是我经常用的一个SP工具类,直接存取就OK,你可以看看这个怎么写的

/**
 * SharedPreferences存储 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数据,没有时为false,方法重载
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

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

    /**
     * 设置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的数据,没有时为null
     */
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    /**
     * 获得String的数据,方法重载
     */
    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getString(key, defValue);
    }

    /**
     * 设置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数据,重载
     */
    public static int getInt(Context context, String key) {
        return getInt(context, key, 1);
    }

    /**
     * 获取int数据,重载
     */
    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = getSp(context);
        return sp.getInt(key, defValue);
    }

    /**
     * 设置int数据,重载
     */
    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();
    }
}

你这只是写数据的SP方法,你怎么读的SP数据

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题