この使用をシングルトンパターンと呼びます。このクラスを使用します。
public class Prefs {
private static final String TAG = "Prefs";
static Prefs singleton = null;
static SharedPreferences preferences;
static SharedPreferences.Editor editor;
private static Gson GSON = new Gson();
Type typeOfObject = new TypeToken<Object>() {
}.getType();
Prefs(Context context) {
preferences = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
editor = preferences.edit();
}
public static Prefs with(Context context) {
if (singleton == null) {
singleton = new Builder(context).build();
}
return singleton;
}
public void save(String key, boolean value) {
editor.putBoolean(key, value).apply();
}
public void save(String key, String value) {
editor.putString(key, value).apply();
}
public void save(String key, int value) {
editor.putInt(key, value).apply();
}
public void save(String key, float value) {
editor.putFloat(key, value).apply();
}
public void save(String key, long value) {
editor.putLong(key, value).apply();
}
public void save(String key, Set<String> value) {
editor.putStringSet(key, value).apply();
}
// to save object in prefrence
public void save(String key, Object object) {
if (object == null) {
throw new IllegalArgumentException("object is null");
}
if (key.equals("") || key == null) {
throw new IllegalArgumentException("key is empty or null");
}
editor.putString(key, GSON.toJson(object)).apply();
}
// To get object from prefrences
public <T> T getObject(String key, Class<T> a) {
String gson = preferences.getString(key, null);
if (gson == null) {
return null;
} else {
try {
return GSON.fromJson(gson, a);
} catch (Exception e) {
throw new IllegalArgumentException("Object storaged with key "
+ key + " is instanceof other class");
}
}
}
public boolean getBoolean(String key, boolean defValue) {
return preferences.getBoolean(key, defValue);
}
public String getString(String key, String defValue) {
return preferences.getString(key, defValue);
}
public int getInt(String key, int defValue) {
return preferences.getInt(key, defValue);
}
public float getFloat(String key, float defValue) {
return preferences.getFloat(key, defValue);
}
public long getLong(String key, long defValue) {
return preferences.getLong(key, defValue);
}
public Set<String> getStringSet(String key, Set<String> defValue) {
return preferences.getStringSet(key, defValue);
}
public Map<String, ?> getAll() {
return preferences.getAll();
}
public void remove(String key) {
editor.remove(key).apply();
}
public void removeAll() {
editor.clear();
editor.apply();
}
private static class Builder {
private final Context context;
public Builder(Context context) {
if (context == null) {
throw new IllegalArgumentException("Context must not be null.");
}
this.context = context.getApplicationContext();
}
/**
* Method that creates an instance of Prefs
*
* @return an instance of Prefs
*/
public Prefs build() {
return new Prefs(context);
}
}
}
kotlinバージョン:
class Prefs internal constructor(context: Context) {
internal var typeOfObject = object : TypeToken<Any>() {
}.type
val all: Map<String, *>
get() = preferences.all
init {
preferences = context.getSharedPreferences(TAG, Context.MODE_PRIVATE)
editor = preferences.edit()
}
fun save(key: String, value: Boolean) {
editor.putBoolean(key, value).apply()
}
fun save(key: String, value: String) {
editor.putString(key, value).apply()
}
fun save(key: String, value: Int) {
editor.putInt(key, value).apply()
}
fun save(key: String, value: Float) {
editor.putFloat(key, value).apply()
}
fun save(key: String, value: Long) {
editor.putLong(key, value).apply()
}
fun save(key: String, value: Set<String>) {
editor.putStringSet(key, value).apply()
}
// to save object in prefrence
fun save(key: String?, `object`: Any?) {
if (`object` == null) {
throw IllegalArgumentException("object is null")
}
if (key == "" || key == null) {
throw IllegalArgumentException("key is empty or null")
}
editor.putString(key, GSON.toJson(`object`)).apply()
}
// To get object from prefrences
fun <T> getObject(key: String, a: Class<T>): T? {
val gson = preferences.getString(key, null)
return if (gson == null) {
null
} else {
try {
GSON.fromJson(gson, a)
} catch (e: Exception) {
throw IllegalArgumentException("Object storaged with key "
+ key + " is instanceof other class")
}
}
}
fun getBoolean(key: String, defValue: Boolean): Boolean {
return preferences.getBoolean(key, defValue)
}
fun getString(key: String, defValue: String): String? {
return preferences.getString(key, defValue)
}
fun getInt(key: String, defValue: Int): Int {
return preferences.getInt(key, defValue)
}
fun getFloat(key: String, defValue: Float): Float {
return preferences.getFloat(key, defValue)
}
fun getLong(key: String, defValue: Long): Long {
return preferences.getLong(key, defValue)
}
fun getStringSet(key: String, defValue: Set<String>): Set<String>? {
return preferences.getStringSet(key, defValue)
}
fun remove(key: String) {
editor.remove(key).apply()
}
fun removeAll() {
editor.clear()
editor.apply()
}
private class Builder(context: Context?) {
private val context: Context
init {
if (context == null) {
throw IllegalArgumentException("Context must not be null.")
}
this.context = context.applicationContext
}
/**
* Method that creates an instance of Prefs
*
* @return an instance of Prefs
*/
fun build(): Prefs {
return Prefs(context)
}
}
companion object {
private val TAG = "Prefs"
lateinit var singleton: Prefs
lateinit var preferences: SharedPreferences
lateinit var editor: SharedPreferences.Editor
private val GSON = Gson()
fun with(context: Context): Prefs {
if (singleton == null) {
singleton = Builder(context).build()
}
return singleton
}
}
}
あなたがオブジェクトを保存するには、Google GSONが必要になります。 これは次のようになります。
Prefs.with(context).save("key","value or object or int or boolean");
希望します。
このメソッドでは、コンテキストを持たないクラスで値を取得することができます。たとえば、あるクラスで「保存」値を取得したいですか? – Frendi
私はあなたに質問があります。あなたはその方法をどこに呼びますか? –
ya、例えばConnection.javaかもしれません..私は私が自分のprefから値を必要とするときに私はその関数を呼び出すときだけ私はパラメータを追加することができます知っているが、私はちょうど好奇心、リークなしで私のprefの値を得ることは可能です。より簡単にコードを書くことができます。 – Frendi