SharedPreferences存储list示例

2021-04-14  本文已影响0人  wenju

SharedPreferences工具类

/**
 * Date: 4/1/21 11:55 AM
 * Description: sp存储数据
 */
public class SharedPreferencesUtil {
    public static final String PREFERENCE_NAME = "SharedPreferencesUtil";

    private SharedPreferencesUtil() {
        throw new AssertionError();
    }

    /**
     * put string preferences .@param context
     *
     * @param key   The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putString(Context context, String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * get string preferences .@param context
     *
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or null. Throws
     * ClassCastException if there is a preference with this name that is
     * not a string
     * @see #getString(Context, String, String)
     */
    public static String getString(Context context, String key) {
        return getString(context, key, "");
    }

    /**
     * get string preferences .@param context
     *
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to common_icon_back if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that is
     * not a string
     */
    public static String getString(Context context, String key,
                                   String defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }

    /**
     * put int preferences .@param context
     *
     * @param key   The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * get int preferences .@param context
     *
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException
     * if there is a preference with this name that is not a int
     * @see #getInt(Context, String, int)
     */
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }

    /**
     * get int preferences .@param context
     *
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to common_icon_back if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that is
     * not a int
     */
    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        try {
            return settings.getInt(key, defaultValue);
        } catch (Exception e) {
            Log.e(PREFERENCE_NAME, e.toString());
        }
        return defaultValue;
    }

    /**
     * put long preferences .@param context
     *
     * @param key   The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putLong(Context context, String key, long value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    /**
     * get long preferences .@param context
     *
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException
     * if there is a preference with this name that is not a long
     * @see #getLong(Context, String, long)
     */
    public static long getLong(Context context, String key) {
        return getLong(context, key, -1);
    }

    /**
     * get long preferences .@param context
     *
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to common_icon_back if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that is
     * not a long
     */
    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        try {
            return settings.getLong(key, defaultValue);
        } catch (Exception e) {
            Log.e(PREFERENCE_NAME, e.toString());
        }
        return defaultValue;

    }

    /**
     * put float preferences .@param context
     *
     * @param key   The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    /**
     * get float preferences .@param context
     *
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException
     * if there is a preference with this name that is not a float
     * @see #getFloat(Context, String, float)
     */
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, 0);
    }

    /**
     * get float preferences .@param context
     *
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to common_icon_back if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that is
     * not a float
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        try {
            return settings.getFloat(key, defaultValue);
        } catch (Exception e) {
            Log.e(PREFERENCE_NAME, e.toString());
        }
        return 0f;
    }

    /**
     * put boolean preferences .@param context
     *
     * @param key   The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * get boolean preferences, default is false .@param context
     *
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or false. Throws
     * ClassCastException if there is a preference with this name that is
     * not a boolean
     * @see #getBoolean(Context, String, boolean)
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * get boolean preferences .@param context
     *
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to common_icon_back if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws
     * ClassCastException if there is a preference with this name that is
     * not a boolean
     */
    public static boolean getBoolean(Context context, String key,
                                     boolean defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        return settings.getBoolean(key, defaultValue);
    }

    /**
     * removeSharedPreferenceByKey:【remove obj in preferences】. <br/>
     * .@param context .@param key .@common_icon_back.<br/>
     */
    public static boolean removeSharedPreferenceByKey(Context context, String key) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.remove(key);
        return editor.commit();
    }

    /**
     * put int preferences .@param context
     *
     * @return True if the new values were successfully written to persistent
     * storage.
     */
    public static boolean clean(Context context) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        Editor editor = settings.edit();
        editor.clear();
        return editor.commit();
    }

}

具体存储list使用

/**
 * Date: 4/1/21 11:55 AM
 * Description: 本地维护下载任务文件名和id
 */
public class FlieDownloadSpDataUtil {
    private static final String FLIEDOWNLOAD_SP_DATA = "FLIEDOWNLOAD_SP_DATA";
    /**
     * @param context 上下文
     */
    public static void setPhoneDevicesPidRules(Context context, ArrayList<FileDownLoadBeen> devicesPidRule) {
        Gson gson = new Gson();
        String data = gson.toJson(devicesPidRule);
        SharedPreferencesUtil.putString(context, FLIEDOWNLOAD_SP_DATA, data);
    }

    /**
     * 获取列表模式
     */
    public static ArrayList<FileDownLoadBeen> getPhoneDevicesPidRules(Context context) {
        ArrayList<FileDownLoadBeen> devicesPidRule = new ArrayList<>();
        String devicesPidRuleJson = SharedPreferencesUtil.getString(context, FLIEDOWNLOAD_SP_DATA,"");
        if(devicesPidRuleJson!=""){
            Gson gson = new Gson();
            devicesPidRule = gson.fromJson(devicesPidRuleJson, new TypeToken<ArrayList<FileDownLoadBeen>>() {}.getType());
        }
        return devicesPidRule;
    }
}
上一篇下一篇

猜你喜欢

热点阅读