Skip to content

Instantly share code, notes, and snippets.

@gfx
Last active October 23, 2023 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfx/8106523 to your computer and use it in GitHub Desktop.
Save gfx/8106523 to your computer and use it in GitHub Desktop.
package com.github.gfx.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class Prefs {
final private SharedPreferences sharedPrefs;
public Prefs(Context context) {
this(PreferenceManager.getDefaultSharedPreferences(context));
}
public Prefs(SharedPreferences sharedPrefs) {
this.sharedPrefs = sharedPrefs;
}
public void put(String key, boolean value) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean(key, value);
editor.apply();
}
public boolean get(String key, boolean defValue) {
return sharedPrefs.getBoolean(key, defValue);
}
public void put(String key, long value) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putLong(key, value);
editor.apply();
}
public long get(String key, long defValue) {
return sharedPrefs.getLong(key, defValue);
}
public void put(String key, float value) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putFloat(key, value);
editor.apply();
}
public float get(String key, float defValue) {
return sharedPrefs.getFloat(key, defValue);
}
public void put(String key, String value) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(key, value);
editor.apply();
}
public String get(String key, String defValue) {
return sharedPrefs.getString(key, defValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment