SharedPreferences storage mode, this tool class is not online everyone with rotten that one, is personally written by the blogger, personally tested, the code is simple and clear, can meet the daily development.

/**
 * Created on 2021/3/30 17:48
 * 
 * @author Gong Youqiang
 */
public class PreferencesUtils{
    public static String PREFERENCE_NAME = "ScarfAndroidCommon";

    private PreferencesUtils(a) {
        throw new AssertionError();
    }

    // put string preferences
    public static boolean putString(Context context, String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    // get string preferences
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    // get string preferences
    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
    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    // get int preferences
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }

    // get int preferences
    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }

    // put long preferences
    public static boolean putLong(Context context, String key, long value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    // get long preferences
    public static long getLong(Context context, String key) {
        return getLong(context, key, -1);
    }

    // get long preferences
    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(key, defaultValue);
    }

    // put float preferences
    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    // get float preferences
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1);
    }

    // get float preferences
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }

    // put boolean preferences
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    // get boolean preferences, default is false
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    // get boolean preferences
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        returnsettings.getBoolean(key, defaultValue); }}Copy the code