2016-03-10 131 views
7

我正在做SharedPreferences助手类,使我的代码看起来不错。SharedPreferences帮手类

public class SharedPreferencesHelper { 
    Context context; 

    public SharedPreferencesHelper(Context context){ 
     this.context = context; 
    } 

    public boolean isLogged(String prefs){ 
     return context.getSharedPreferences(prefs,Context.MODE_PRIVATE) 
         .getBoolean("LOGGED",false); 
    } 

    public void setLogged(String prefs){ 
     context.getSharedPreferences(prefs,Context.MODE_PRIVATE) 
       .edit().putBoolean("LOGGED",true).apply(); 
    } 
} 

的问题是我应该做这些方法静态的,每个方法初始化SharedPreferences或更好地离开它不是静态的,从我的其他类调用一次SharedPreferencesHelper类?谢谢

+0

如果你把它变成静态的,它会有什么区别吗? –

回答

3

我不会保留对上下文的引用。我宁愿将SharedPreference及其Editor作为您的助手类的静态成员。这样,每次您需要读取/写入SharedPreference时,都不需要例示SharedPreferencesHelper。更进一步的将使用应用程序Context(带有您的自定义应用程序子类)来初始化SharedPreferenceEditor,这是您第一次访问帮助程序本身。这就是我如何塑造它

+0

请你可以告诉我的代码? –

+0

使用dagger2和单身人士会有所帮助。你可以在任何地方注入共享偏好 – Raghunandan

+0

@Raghunandan谢谢你的提示。我从来没有用过匕首,所以我有点盲目。关于单身人士,我不认为有必要。最后,op想要公开的方法写入从sharedpreference – Blackbelt

1

我会使用静态类,如果Context是一个“全球”的上下文,并从上下文获得值是可怕的长和(有点)邪恶。这样一来,从静态类中获取值将变得更加容易,而不会让您在重复执行整个代码空间内的相同操作时出错而不会出错。

至于把你静态SharedPreferencesHelper,一个好办法:

public class SharedPreferencesHelper { 

    private SharedPreferencesHelper(Context context){ 
    } 

    private static void ensureNotNull(Context context) { 
     if (context == null) { 
      throw new IllegalArgumentException("Context is null."); 
     } 
    } 

    public static boolean isLogged(Context context, String prefs){ 
     ensureNotNull(context); 
     return context.getSharedPreferences(prefs,Context.MODE_PRIVATE) 
         .getBoolean("LOGGED",false); 
    } 

    public static void setLogged(Context context, String prefs){ 
     ensureNotNull(context); 
     context.getSharedPreferences(prefs,Context.MODE_PRIVATE) 
       .edit().putBoolean("LOGGED",true).apply(); 
    } 
} 
-1

下面这个类是帮助你。

import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.preference.PreferenceManager; 
//import android.preference.PreferenceManager; 

public class SharedPreference { 

    private static SharedPreference sharedPreference; 
    public static final String PREFS_NAME = "AOP_PREFS"; 
    public static final String PREFS_KEY = "AOP_PREFS_String"; 



    public static SharedPreference getInstance() 
    { 
     if (sharedPreference == null) 
     { 
      sharedPreference = new SharedPreference(); 
     } 
     return sharedPreference; 
    } 

    public SharedPreference() { 
     super(); 
    } 

    public void save(Context context, String text , String Key) { 
     SharedPreferences settings; 
     Editor editor; 

     //settings = PreferenceManager.getDefaultSharedPreferences(context); 
     settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1 
     editor = settings.edit(); //2 

     editor.putString(Key, text); //3 

     editor.commit(); //4 
    } 

    public String getValue(Context context , String Key) { 
     SharedPreferences settings; 
     String text = ""; 
     // settings = PreferenceManager.getDefaultSharedPreferences(context); 
     settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     text = settings.getString(Key, ""); 
     return text; 
    } 

    public void clearSharedPreference(Context context) { 
     SharedPreferences settings; 
     Editor editor; 

     //settings = PreferenceManager.getDefaultSharedPreferences(context); 
     settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     editor = settings.edit(); 

     editor.clear(); 
     editor.commit(); 
    } 

    public void removeValue(Context context , String value) { 
     SharedPreferences settings; 
     Editor editor; 

     settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
     editor = settings.edit(); 

     editor.remove(value); 
     editor.commit(); 
    } 
} 

你可以得到像这样的价值。

String KeeLogin = SharedPreference.getInstance().getValue(getApplicationContext(), "YOUR_KEY"); 

和存储数据的筛选

SharedPreference.getInstance().save(LoginScreen.this,"VALUE","YOUR_KEY"); 

希望它的帮助你:)

+0

是的,它会毫无疑问,但我试图让我的代码看起来很简单。无论如何,谢谢 –

2

使用此:

public class SharedPreferencesHelper { 

public static final String FILE_NAME = "APP_PREFERENCES"; 

    public static void put(Context context, String key, Object object) { 

    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sp.edit(); 
    if (object instanceof String) { 
     editor.putString(key, (String) object); 
    } else if (object instanceof Integer) { 
     editor.putInt(key, (Integer) object); 
    } else if (object instanceof Boolean) { 
     editor.putBoolean(key, (Boolean) object); 
    } else if (object instanceof Float) { 
     editor.putFloat(key, (Float) object); 
    } else if (object instanceof Long) { 
     editor.putLong(key, (Long) object); 
    } else { 
     editor.putString(key, object.toString()); 
    } 
    SharedPreferencesCompat.apply(editor); 
} 

    public static Object get(Context context, String key, Object defaultObject) { 

    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); 

    if (defaultObject instanceof String) { 
     return sp.getString(key, (String) defaultObject); 
    } else if (defaultObject instanceof Integer) { 
     return sp.getInt(key, (Integer) defaultObject); 
    } else if (defaultObject instanceof Boolean) { 
     return sp.getBoolean(key, (Boolean) defaultObject); 
    } else if (defaultObject instanceof Float) { 
     return sp.getFloat(key, (Float) defaultObject); 
    } else if (defaultObject instanceof Long) { 
     return sp.getLong(key, (Long) defaultObject); 
    } 

    return null; 
} 

public static void remove(Context context, String key) { 
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sp.edit(); 
    editor.remove(key); 
    SharedPreferencesCompat.apply(editor); 
} 

public static void clear(Context context) { 
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sp.edit(); 
    editor.clear(); 
    SharedPreferencesCompat.apply(editor); 
} 

public static boolean contains(Context context, String key) { 
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); 
    return sp.contains(key); 
} 

public static Map<String, ?> getAll(Context context) { 
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); 
    return sp.getAll(); 
} 



private static class SharedPreferencesCompat { 
    private static final Method sApplyMethod = findApplyMethod(); 

    @SuppressWarnings({"unchecked", "rawtypes"}) 
    private static Method findApplyMethod() { 
     try { 
      Class clz = SharedPreferences.Editor.class; 
      return clz.getMethod("apply"); 
     } catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    public static void apply(SharedPreferences.Editor editor) { 
     try { 
      if (sApplyMethod != null) { 
       sApplyMethod.invoke(editor); 
       return; 
      } 
     } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { 
      e.printStackTrace(); 
     } 
     editor.commit(); 


    } 
} 
} 
+0

出了什么问题? – MrZ

0

我结束了单例模式,只保留一个实例当时:

import android.content.Context; 
import android.support.annotation.NonNull; 

public class SharedPrefsUtil { 
    private static SharedPrefsUtil instance; 
    private static final String PREFS_NAME = "default_preferences"; 

    public synchronized static SharedPrefsUtil getInstance() { 
     if (instance == null) { 
      instance = new SharedPrefsUtil(); 
     } 
     return instance; 
    } 

    private SharedPrefsUtil() { 
    } 

    public boolean isLoggedIn(@NonNull Context context) { 
     return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) 
       .getBoolean("LOGGED", false); 
    } 

    public void setLoggedIn(@NonNull Context context, boolean value) { 
     context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) 
       .edit().putBoolean("LOGGED", value).apply(); 
    } 
} 

请注意,使用Dagger库可以轻松实现同一单例。