创建 SharedPreferencesManager.java
parent
b64cb19e17
commit
e5d8b244d1
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.nbee.echolink.utils;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
public class SharedPreferencesManager {
|
||||||
|
|
||||||
|
private static final String PREFS_NAME = "MyAppPrefs";
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
public SharedPreferencesManager(Context context) {
|
||||||
|
this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putString(String key, String value) {
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putString(key, value);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString(String key,String defaultValue) {
|
||||||
|
return sharedPreferences.getString(key,defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putBoolean(String key, boolean value) {
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putBoolean(key, value);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getBoolean(String key, boolean defaultValue) {
|
||||||
|
return sharedPreferences.getBoolean(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putInt(String key, int value) {
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putInt(key, value);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getInt(String key, int defaultValue) {
|
||||||
|
return sharedPreferences.getInt(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加更多的方法来处理其他类型,如 putLong, getLong, putFloat, getFloat 等。
|
||||||
|
|
||||||
|
public void remove(String key) {
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.remove(key);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearAll() {
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.clear();
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue