public abstract class PreferencesManager extends Object
SharedPreferences of an Android application.
putInt(String, int) - getInt(String, int)putFloat(String, float) - getInt(String, int)putLong(String, long) - getLong(String, long)putBoolean(String, boolean) - getBoolean(String, boolean)putString(String, String) - getString(String, String)ArrayPreference and ListPreference.
SharedPreference. You can use any implementation
of SharedPreference class to represent any of your shared preference values. The following methods
can be used to store/retrieve actual values of these preferences:
public class AppPreferences extends PreferencesManager {
// Key for the first app launch preference.
private static final String KEY_APP_FIRST_LAUNCH = ".AppFirstLaunch";
// Name of the shared preferences.
private final String mPrefsName;
// Creates a new instance of AppPreferences manager.
public AppPreferences(Context context) {
super(context);
this.mPrefsName = getSharedPreferencesName();
}
// Stores a boolean flag into shared preferences indicating whether this is a first launch
// of this app or not.
public void saveIsAppFirstLaunch(boolean first) {
putBoolean(mPrefsName + KEY_APP_FIRST_LAUNCH, first);
}
// Returns a boolean flag from shared preferences indicating whether this is a first launch
// of this app or not.
public boolean isAppFirstLaunch() {
return getBoolean(mPrefsName + KEY_APP_FIRST_LAUNCH, true);
}
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
PreferencesManager.Mode
Defines an annotation for determining set of allowed flags for
setMode(int) method. |
| Modifier and Type | Field and Description |
|---|---|
protected Context |
mContext
Application context used to access preferences.
|
static int |
MODE_APPEND
Wrapped mode:
Context.MODE_APPEND |
static int |
MODE_ENABLE_WRITE_AHEAD_LOGGING
Wrapped mode:
Context.MODE_ENABLE_WRITE_AHEAD_LOGGING |
static int |
MODE_MULTI_PROCESS
Wrapped mode:
Context.MODE_MULTI_PROCESS |
static int |
MODE_PRIVATE
Wrapped mode:
Context.MODE_PRIVATE |
| Modifier | Constructor and Description |
|---|---|
protected |
PreferencesManager(Context context)
Same as
PreferencesManager(Context, String) with default name for
preferences (like the one provided by Android framework). |
protected |
PreferencesManager(Context context,
String preferencesName)
Same as
PreferencesManager(Context, String, int) with Context.MODE_PRIVATE
creation mode. |
protected |
PreferencesManager(Context context,
String preferencesName,
int mode)
Creates a new instance of PreferencesManager with the given shared preferences name and
preferences file creation mode.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(String key)
Checks whether there is contained some value for the specified key in the share
preferences.
|
boolean |
containsPreference(SharedPreference preference)
Checks whether value of the given preference is contained within the shared preferences.
|
static String |
defaultSharedPreferencesName(Context context)
Creates a default name for shared preferences (like the one created by
PreferenceManager). |
boolean |
getBoolean(String key,
boolean defValue)
Returns the
boolean value mapped in the shared preferences under the given key. |
float |
getFloat(String key,
float defValue)
Returns the
float value mapped in the shared preferences under the given key. |
int |
getInt(String key,
int defValue)
Returns the
int value mapped in the shared preferences under the given key. |
long |
getLong(String key,
long defValue)
Returns the
long value mapped in the shared preferences under the given key. |
protected int |
getMode()
Returns the actual file creation mode for shared preferences.
|
<Type> Type |
getPreference(SharedPreference<Type> preference)
Obtains the value of the given preference from shared preferences.
|
SharedPreferences |
getSharedPreferences()
Returns the instance of SharedPreferences depends on the current file creation mode and
preferences name.
|
protected String |
getSharedPreferencesName()
Returns the actual name for shared preferences.
|
String |
getString(String key,
String defValue)
Returns the
String value mapped in the shared preferences under the given key. |
protected boolean |
isCachingEnabled()
Returns flag indicating whether the caching of
SharedPreference's
values is enabled or not. |
protected String |
key(int resId)
Returns a string key for the specified resId.
|
boolean |
putBoolean(String key,
boolean value)
Saves the given
boolean value into shared preferences. |
boolean |
putFloat(String key,
float value)
Saves the given
float value into shared preferences. |
boolean |
putInt(String key,
int value)
Saves the given
int value into shared preferences. |
boolean |
putLong(String key,
long value)
Saves the given
long value into shared preferences. |
<Type> boolean |
putPreference(SharedPreference<Type> preference,
Type value)
Saves the given value using the specified preference into shared preferences.
|
boolean |
putString(String key,
String value)
Saves the given
String value into shared preferences. |
void |
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
|
boolean |
remove(String key)
Removes current value of a preference with the specified key from the shared preferences.
|
boolean |
removePreference(SharedPreference preference)
Removes current value of the given preference from the shared preferences.
|
protected void |
setCachingEnabled(boolean enabled)
Enables/disables the caching
SharedPreference's values. |
protected void |
setMode(int mode)
Sets the actual file creation mode for shared preferences.
|
protected void |
setSharedPreferencesName(String name)
Sets the actual name for shared preferences.
|
void |
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
|
public static final int MODE_PRIVATE
Context.MODE_PRIVATEpublic static final int MODE_MULTI_PROCESS
Context.MODE_MULTI_PROCESSpublic static final int MODE_ENABLE_WRITE_AHEAD_LOGGING
Context.MODE_ENABLE_WRITE_AHEAD_LOGGINGpublic static final int MODE_APPEND
Context.MODE_APPENDprotected final Context mContext
protected PreferencesManager(@NonNull Context context)
PreferencesManager(Context, String) with default name for
preferences (like the one provided by Android framework).protected PreferencesManager(@NonNull Context context, @NonNull String preferencesName)
PreferencesManager(Context, String, int) with Context.MODE_PRIVATE
creation mode.protected PreferencesManager(@NonNull Context context, @NonNull String preferencesName, int mode)
context - Valid context to access shared preferences.preferencesName - Name for shared preferences file.mode - Shared preferences will be created in this mode.@NonNull public static String defaultSharedPreferencesName(@NonNull Context context)
PreferenceManager).context - Context for which the name should be created.protected final int getMode()
MODE_PRIVATE by default.Context,
setMode(int)protected final void setMode(int mode)
mode - Preferences creation mode.getMode()protected final void setSharedPreferencesName(@Nullable String name)
Note, that you can set this to null to retrieve always default shared preferences.
name - The desired name for shared preferences.getSharedPreferencesName(),
getSharedPreferences()@Nullable protected final String getSharedPreferencesName()
setSharedPreferencesName(String)@NonNull public SharedPreferences getSharedPreferences()
setMode(int),
setSharedPreferencesName(String)protected final void setCachingEnabled(boolean enabled)
SharedPreference's values.enabled - True to enable caching, false to disable.isCachingEnabled()protected final boolean isCachingEnabled()
SharedPreference's
values is enabled or not.True if caching is enabled, false otherwise.setCachingEnabled(boolean)@NonNull protected final String key(@StringRes int resId)
resId - Resource id of the desired preference key.public void registerOnSharedPreferenceChangeListener(@NonNull SharedPreferences.OnSharedPreferenceChangeListener listener)
listener - Listener callback to register up on the current shared preferences.getSharedPreferences(),
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener)public void unregisterOnSharedPreferenceChangeListener(@NonNull SharedPreferences.OnSharedPreferenceChangeListener listener)
listener - Listener callback to un-register up on the current shared preferences.getSharedPreferences(),
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener)public final boolean putInt(@NonNull String key, int value)
int value into shared preferences.key - The key under which will be the specified value mapped in the shared preferences.value - Value to save.True if saving succeeded, false otherwise.getInt(String, int),
contains(String),
remove(String)public final int getInt(@NonNull String key, int defValue)
int value mapped in the shared preferences under the given key.key - The key under which is the requested value mapped in the shared preferences.defValue - Default value to return if there is no mapping for the specified key yet.int value obtained from the shared preferences or defValue if
there is no mapping for the given key.putInt(String, int)public final boolean putFloat(@NonNull String key, float value)
float value into shared preferences.key - The key under which will be the specified value mapped in the shared preferences.value - Value to save.True if saving succeeded, false otherwise.getFloat(String, float),
contains(String),
remove(String)public final float getFloat(@NonNull String key, float defValue)
float value mapped in the shared preferences under the given key.key - The key under which is the requested value mapped in the shared preferences.defValue - Default value to return if there is no mapping for the specified key yet.float value obtained from the shared preferences or
defValue if there is no mapping for the specified key.putFloat(String, float)public final boolean putLong(@NonNull String key, long value)
long value into shared preferences.key - The key under which will be the specified value mapped in the shared preferences.value - Value to save.True if saving succeeded, false otherwise.getLong(String, long),
contains(String),
remove(String)public final long getLong(@NonNull String key, long defValue)
long value mapped in the shared preferences under the given key.key - The key under which is the requested value mapped in the shared preferences.defValue - Default value to return if there is no mapping for the specified key yet.long value obtained from the shared preferences or
defValue if there is no mapping for the specified key.putLong(String, long)public final boolean putBoolean(@NonNull String key, boolean value)
boolean value into shared preferences.key - The key under which will be the specified value mapped in the shared preferences.value - Value to save.True if saving succeeded, false otherwise.getBoolean(String, boolean),
contains(String),
remove(String)public final boolean getBoolean(@NonNull String key, boolean defValue)
boolean value mapped in the shared preferences under the given key.key - The key under which is the requested value mapped in the shared preferences.defValue - Default value to return if there is no mapping for the specified key yet.boolean value obtained from the shared preferences
or defValue if there is no mapping for the specified key.putBoolean(String, boolean)public final boolean putString(@NonNull String key, @Nullable String value)
String value into shared preferences.key - The key under which will be the specified value mapped in the shared preferences.value - Value to save.True if saving succeeded, false otherwise.getString(String, String),
contains(String),
remove(String)public final String getString(@NonNull String key, @Nullable String defValue)
String value mapped in the shared preferences under the given key.key - The key under which is the requested value mapped in the shared preferences.defValue - Default value to return if there is no mapping for the specified key yet.String value obtained from the shared preferences or
defValue if there is no mapping for the specified key.putString(String, String)public final boolean contains(@NonNull String key)
key - Key of the desired preference of which value to check.True if some value for the requested key is stored, false otherwise.remove(String)public final boolean remove(@NonNull String key)
key - Key of the desired preference of which value (entry) should be removed.True if current value of shared preference has been removed, false if
some error occurred.contains(String)public final <Type> boolean putPreference(@NonNull SharedPreference<Type> preference, @Nullable Type value)
preference - Preference of which value should be saved.value - The value of preference to save into shared preferences.True if save operation succeeded, false otherwise.getPreference(SharedPreference),
containsPreference(SharedPreference),
removePreference(SharedPreference)public final <Type> Type getPreference(@NonNull SharedPreference<Type> preference)
preference - Preference of which value should be obtained.putPreference(SharedPreference, Object)public final boolean containsPreference(@NonNull SharedPreference preference)
preference - The desired preference of which value to check.True if some value for the specified preference is stored, false otherwise.putPreference(SharedPreference, Object),
getPreference(SharedPreference),
removePreference(SharedPreference)public final boolean removePreference(@NonNull SharedPreference preference)
preference - The desired preference of which value (entry) should be removed.True if current value of shared preference has been removed, false if
some error occurred.putPreference(SharedPreference, Object),
getPreference(SharedPreference),
containsPreference(SharedPreference)