Lin*_*nxy 14 java singleton android preferences sharedpreferences
我需要一个处理我的SharedPreferences的类,我想出了3种方法,但经过一些研究后,似乎大多数都被认为是"反模式".
输入1
public final class MyPrefs {
private MyPrefs(){ throw new AssertionError(); }
public static void setFavoriteColor(Context context, String value){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("color_key", value).apply();
}
public static void setFavoriteAnimal(Context context, String value){
// ...
}
// ...
}
/* Usage */
MyPrefs.setFavoriteColor(this, "yellow");
// Reason why it might be considered "Bad"
// Class is not OO, just collection of static methods. "Utility Class"
Run Code Online (Sandbox Code Playgroud)
类型2
public class MyPrefs {
private SharedPreferences mPreferences;
private static volatile MyPrefs sInstance;
public static MyPrefs getInstance(Context context){
if(sInstance == null){
synchronized(MyPrefs.class){
if(sInstance == null){
sInstance = new MyPrefs(context);
}
}
}
return sInstance;
}
private MyPrefs(Context context){
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setFavoriteColor(String value){
mPreferences.edit().putString("color_key", value).apply();
}
public void setFavoriteAnimal(Context context, String value){
// ...
}
// ...
}
/* Usage */
MyPrefs myPrefs = MyPrefs.getInstance(this);
myPrefs.setFavoriteColor("red");
// Reason why it might be considered "Bad"
// Singleton's are frowned upon especially
// in android because they can cause problems and unexpected bugs.
Run Code Online (Sandbox Code Playgroud)
输入3
public class MyPrefs {
SharedPreferences mPreferences;
public MyPrefs(Context context){
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setFavoriteColor(String value){
mPreferences.edit().putString("color_key", value).apply();
}
public void setFavoriteAnimal(Context context, String value){
// ...
}
// ...
}
/* Usage */
MyPrefs myPrefs = new MyPrefs(this);
myPrefs.setFavoriteColor("green");
// Reason why it might be considered "Bad"
// Lots of boilerplate and must create object every
// time you want to save a preference.
Run Code Online (Sandbox Code Playgroud)
现在我的偏好包装器显然不仅包含2个setter,它们有很多getter和setter在保存值之前进行一些侧面处理,因此在主要活动中保存和处理首选项会导致很多乱码和错误.
现在,哪些方法不会对性能产生负面影响/导致意外错误?
类型1: -
在类型1中,你直接使用这个class method,这个最好........
第2类: -
在2型,有一个Static Variable会导致在MemoryLeakException你的应用程序.如果你想使用类型2,那么INSTANCE无论何时使用这个类,你都会使变量为null(这些可以解决问题MemoryLeakException).......
类型3: -
在类型3中,您必须创建Heap Memory(在实例中使用Ram内存直到其范围结束)或 new Instance类,只要您想使用此类.如果你必须class methods在单个时间内使用它,这个课程会有所帮助Activity.......
使用这个类来简单使用
SharePrefernce.....
public class Utility {
public static boolean getBoolean(Context context, String key) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(key, false);
}
public static String getString(Context context, String key) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, "");
}
public static int getInt(Context context, String key) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(key, -1);
}
public static void setString(Context context, String key, String value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).commit();
}
public static void setBoolean(Context context, String key, boolean value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).commit();
}
}
Run Code Online (Sandbox Code Playgroud)
用于设置字符串....
Utility.setString(this,"token","your token");
Run Code Online (Sandbox Code Playgroud)
并获得字符串......
Utility.getString(this,"token");
Run Code Online (Sandbox Code Playgroud)
注意: - 在此条款中,您不必创建任何 Heap Memory OR Static Variable.