public class PreferencesManager extends SimpleSharedPreferencesFacade
SharedPreferences of an Android application.
SimpleSharedPreferencesFacade.putInt(String, int) - SimpleSharedPreferencesFacade.getInt(String, int)SimpleSharedPreferencesFacade.putFloat(String, float) - SimpleSharedPreferencesFacade.getInt(String, int)SimpleSharedPreferencesFacade.putLong(String, long) - SimpleSharedPreferencesFacade.getLong(String, long)SimpleSharedPreferencesFacade.putBoolean(String, boolean) - SimpleSharedPreferencesFacade.getBoolean(String, boolean)SimpleSharedPreferencesFacade.putString(String, String) - SimpleSharedPreferencesFacade.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 class |
PreferencesManager.Builder
Builder that may be used to creates new instances of PreferencesManager with a desired configuration.
|
static interface |
PreferencesManager.Mode
Deprecated.
Use
@SharedPreferencesPolicy.FileMode instead. |
| Modifier and Type | Field and Description |
|---|---|
protected android.content.Context |
mContext
Context that has been specified during initialization of this manager instance.
|
static int |
MODE_APPEND
Deprecated.
Use
SharedPreferencesPolicy.FILE_MODE_APPEND instead. |
static int |
MODE_ENABLE_WRITE_AHEAD_LOGGING
Deprecated.
This flag is appropriate only for database files.
|
static int |
MODE_MULTI_PROCESS
Deprecated.
Deprecated due to deprecation in the framework. See
Context.MODE_MULTI_PROCESS. |
static int |
MODE_PRIVATE
Deprecated.
Use
SharedPreferencesPolicy.FILE_MODE_PRIVATE instead. |
| Modifier | Constructor and Description |
|---|---|
|
PreferencesManager(android.content.Context context)
Deprecated.
Use
PreferencesManager(Builder) instead. |
|
PreferencesManager(android.content.Context context,
String preferencesName)
Deprecated.
Use
PreferencesManager(Builder) instead. |
|
PreferencesManager(android.content.Context context,
String preferencesName,
int preferencesFileMode)
Deprecated.
Use
PreferencesManager(Builder) instead. |
protected |
PreferencesManager(PreferencesManager.Builder builder)
todo:
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
containsPreference(SharedPreference preference)
Checks whether there is value associated with the specified preference contained
within
SharedPreferences that are managed by this manager. |
static String |
defaultSharedPreferencesName(android.content.Context context)
Deprecated.
|
protected int |
getMode()
Deprecated.
Use
getPreferencesFileMode() instead. |
<T> T |
getPreference(SharedPreference<T> preference)
Obtains the value for the given preference from
SharedPreferences that are
managed by this manager. |
int |
getPreferencesFileMode()
Returns the file mode of
SharedPreferences that are managed by this manager. |
String |
getPreferencesName()
Returns the name of
SharedPreferences that are managed by this manager. |
android.content.SharedPreferences |
getSharedPreferences()
Deprecated.
Use
SimpleSharedPreferencesFacade.getPreferences() instead. |
protected String |
getSharedPreferencesName()
Deprecated.
Use
getPreferencesName() instead. |
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.
|
<T> boolean |
putPreference(SharedPreference<T> preference,
T value)
Persists the given value for the specified preference into
SharedPreferences
that are managed by this manager. |
boolean |
removePreference(SharedPreference preference)
Removes a value for the specified preference from
SharedPreferences that
are managed by this manager. |
void |
setCachingEnabled(boolean enabled)
Enables/disables the caching
SharedPreference's values. |
protected void |
setMode(int mode)
Deprecated.
The new implementation of preferences manager does not support changing of preferences
file mode after the manager has been initialized.
|
protected void |
setSharedPreferencesName(String name)
Deprecated.
The new implementation of preferences manager does not support changing of preferences
name after the manager has been initialized.
|
contains, getBoolean, getFloat, getInt, getLong, getPreferences, getString, getStringSet, putBoolean, putFloat, putInt, putLong, putString, putStringSet, registerOnSharedPreferenceChangeListener, remove, removeAll, unregisterOnSharedPreferenceChangeListener@Deprecated public static final int MODE_PRIVATE
SharedPreferencesPolicy.FILE_MODE_PRIVATE instead.
Wrapped mode: Context.MODE_PRIVATE
@Deprecated public static final int MODE_MULTI_PROCESS
Context.MODE_MULTI_PROCESS.
Wrapped mode: Context.MODE_MULTI_PROCESS
@Deprecated public static final int MODE_ENABLE_WRITE_AHEAD_LOGGING
Wrapped mode: Context.MODE_ENABLE_WRITE_AHEAD_LOGGING
@Deprecated public static final int MODE_APPEND
SharedPreferencesPolicy.FILE_MODE_APPEND instead.
Wrapped mode: Context.MODE_APPEND
protected final android.content.Context mContext
@Deprecated public PreferencesManager(android.content.Context context)
PreferencesManager(Builder) instead.
Same as PreferencesManager(Context, String) with default name for
preferences (like the one provided by Android framework).
@Deprecated public PreferencesManager(android.content.Context context, String preferencesName)
PreferencesManager(Builder) instead.
Same as PreferencesManager(Context, String, int) with Context.MODE_PRIVATE
creation mode.
@Deprecated public PreferencesManager(android.content.Context context, String preferencesName, int preferencesFileMode)
PreferencesManager(Builder) instead.Creates a new instance of PreferencesManager with the given shared preferences name and preferences file creation mode.
context - Valid context to access shared preferences.preferencesName - Name for shared preferences file.preferencesFileMode - Shared preferences will be created in this mode.protected PreferencesManager(PreferencesManager.Builder builder)
builder - public final String getPreferencesName()
SharedPreferences that are managed by this manager.PreferencesManager.Builder.preferencesName(String),
SimpleSharedPreferencesFacade.getPreferences(),
getPreferencesFileMode()public final int getPreferencesFileMode()
SharedPreferences that are managed by this manager.PreferencesManager.Builder.preferencesFileMode(int),
SimpleSharedPreferencesFacade.getPreferences(),
getPreferencesName()public final void setCachingEnabled(boolean enabled)
SharedPreference's values.enabled - True to enable caching, false to disable.isCachingEnabled()public final boolean isCachingEnabled()
SharedPreference's
values is enabled or not.True if caching is enabled, false otherwise.setCachingEnabled(boolean)protected final String key(int resId)
resId - Resource id of the desired preference key.public final <T> boolean putPreference(SharedPreference<T> preference, T value)
SharedPreferences
that are managed by this manager.T - Type of the value associated with the preference.preference - Preference for which to persist the value.value - The value to be persisted.True if put has been successful, false otherwise.getPreference(SharedPreference),
containsPreference(SharedPreference),
removePreference(SharedPreference),
SharedPreference.updateValue(Object),
SharedPreference.putIntoPreferences(SharedPreferences)public final <T> T getPreference(SharedPreference<T> preference)
SharedPreferences that are
managed by this manager.T - Type of the value associated with the preference.preference - Preference for which to obtain its associated value.null if there is no value persisted
for the preference yet.putPreference(SharedPreference, Object),
SimpleSharedPreferencesFacade.contains(String),
SharedPreference.getFromPreferences(SharedPreferences)public final boolean containsPreference(SharedPreference preference)
SharedPreferences that are managed by this manager.preference - The desired preference of which value's presence to check.True if there is value contained for key of the specified preference,
false otherwise.putPreference(SharedPreference, Object),
getPreference(SharedPreference),
removePreference(SharedPreference)public final boolean removePreference(SharedPreference preference)
SharedPreferences that
are managed by this manager.preference - The preference for which to remove its associated value.True if removal has been successful, false otherwise.putPreference(SharedPreference, Object),
getPreference(SharedPreference),
containsPreference(SharedPreference)@Deprecated public static String defaultSharedPreferencesName(android.content.Context context)
SharedPreferencesPolicy.defaultPreferencesName(Context) instead.
Creates a default name for shared preferences (like the one created by PreferenceManager).
context - Context for which the name should be created.@Deprecated protected final void setMode(int mode)
Sets the actual file creation mode for shared preferences. This mode is used whenever the instance of shared preferences is obtained to put/retrieve data to/from them.
mode - Preferences creation mode.getMode()@Deprecated protected final int getMode()
getPreferencesFileMode() instead.Returns the actual file creation mode for shared preferences.
MODE_PRIVATE by default.Context,
setMode(int)@Deprecated protected final void setSharedPreferencesName(String name)
Sets the actual name for shared preferences. This name is used whenever the instance of shared preferences is needed to put/retrieve data to/from it.
Note, that you can set this to null to retrieve always default shared preferences.
name - The desired name for shared preferences.getSharedPreferencesName(),
getSharedPreferences()@Deprecated protected final String getSharedPreferencesName()
getPreferencesName() instead.Returns the actual name for shared preferences.
setSharedPreferencesName(String)@Deprecated public android.content.SharedPreferences getSharedPreferences()
SimpleSharedPreferencesFacade.getPreferences() instead.Returns the instance of SharedPreferences depends on the current file creation mode and preferences name. If there is not currently provided name for shared preferences, the default one will be used.
setMode(int),
setSharedPreferencesName(String)