Mess with the shared preferences of android - which function to use?

前端 未结 4 1903
旧时难觅i
旧时难觅i 2020-12-17 04:47

Here\'s a standard task: store some values in the application\'s shared preferences in order to be able to retrieve it later on. But one will discover that there are 3 funct

4条回答
  •  鱼传尺愫
    2020-12-17 05:24

    I am sharing mine, hope it will make life easy -

        public class SharedPreferenceStore {
    
        public static void deleteValue(Context context, String key) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            Editor editor = preferences.edit();
            editor.remove(key).apply();
        }
    
        public static void storeValue(Context context, String key, String value) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            Editor editor = preferences.edit();
            editor.putString(key, value).apply();
        }
    
        public static void storeValue(Context context, String key, boolean value) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            Editor editor = preferences.edit();
            editor.putBoolean(key, value).apply();
        }
    
        public static void storeValue(Context context, String key, double value) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            String doubleVal = String.valueOf(value);
            Editor editor = preferences.edit();
            editor.putString(key, doubleVal).apply();
        }
    
        public static void storeValue(Context context, String key, float value) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            Editor editor = preferences.edit();
            editor.putFloat(key, value).apply();
        }
    
        public static void storeValue(Context context, String key, long value) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            Editor editor = preferences.edit();
            editor.putLong(key, value).apply();
        }
    
        public static void storeValue(Context context, String key, int value) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            Editor editor = preferences.edit();
            editor.putInt(key, value).apply();
        }
    
        /*************************
         * GET Methods
         ***************************************/
        public static String getValue(Context context, String key, String defValue) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getString(key, defValue);
        }
    
        public static boolean getValue(Context context, String key, boolean defValue) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getBoolean(key, defValue);
        }
    
        public static double getValue(Context context, String key, double defValue) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            String doubleVal = String.valueOf(defValue);
            return Double.parseDouble(preferences.getString(key, doubleVal));
        }
    
        public static float getValue(Context context, String key, float defValue) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getFloat(key, defValue);
        }
    
        public static long getValue(Context context, String key, long defValue) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getLong(key, defValue);
        }
    
        public static int getValue(Context context, String key, int defValue) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            return preferences.getInt(key, defValue);
        }
    }
    

    Just use the class and forget about all complications of SharedPrefences. Hope it will help you :)

提交回复
热议问题