Last active
October 23, 2023 07:47
-
-
Save gfx/8106523 to your computer and use it in GitHub Desktop.
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
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