2016-03-10 77 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庫可以輕鬆實現同一單例。