Last active
November 30, 2016 11:18
-
-
Save Gnzlt/211c381303879e4d895a to your computer and use it in GitHub Desktop.
Generic Class to write/save Android preferences data using SharedPreferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
public class PrefsUtils { | |
private static SharedPreferences getPreferences(Context context) { | |
return PreferenceManager.getDefaultSharedPreferences(context); | |
} | |
private static SharedPreferences.Editor getPreferencesEditor(Context context) { | |
return getPreferences(context).edit(); | |
} | |
public static String getPreferenceValue(Context context, String key, String defValue) { | |
return getPreferences(context).getString(key, defValue); | |
} | |
public static int getPreferenceValue(Context context, String key, int defValue) { | |
return getPreferences(context).getInt(key, defValue); | |
} | |
public static boolean getPreferenceValue(Context context, String key, boolean defValue) { | |
return getPreferences(context).getBoolean(key, defValue); | |
} | |
public static void setPreferenceValue(Context context, String key, String prefsValue) { | |
getPreferencesEditor(context).putString(key, prefsValue).apply(); | |
} | |
public static void setPreferenceValue(Context context, String key, int prefsValue) { | |
getPreferencesEditor(context).putInt(key, prefsValue).apply(); | |
} | |
public static void setPreferenceValue(Context context, String key, boolean prefsValue) { | |
getPreferencesEditor(context).putBoolean(key, prefsValue).apply(); | |
} | |
public static boolean containsPreferenceKey(Context context, String key) { | |
return getPreferences(context).contains(key); | |
} | |
public static void removePreferenceValue(Context context, String key) { | |
getPreferencesEditor(context).remove(key).apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment