public class PreferencePortal
extends java.lang.Object
SharedPreferences
; most notably:
SharedPreferences.Editor
; these methods can then be chained
with apply()
or
commit()
(though the latter
may draw the ire of Lint without a
@SuppressLint( "CommitPrefEdits" ) annotation.
An app may choose to extend this class to further provide easy methods for getting and setting specific preferences.
Note that this class is basically syntactic sugar; an app can easily do all these things itself, instead. This author accepts no responsibility for any taunting or other lack of respect for using this class to clean up what would otherwise be a pretty tedious coding task. However, in the case of stringified integer values, the class does provide static versions of those methods, for those who don't want to lean heavily on the whole class.
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_BOOLEAN
The default value for Boolean preferences.
|
static int |
DEFAULT_INT_NOT_SET
A default value for integer fields, which indicates "not set".
|
static int |
DEFAULT_INT_ZERO
The usual default value for integer fields.
|
static java.lang.String |
DEFAULT_STRING
The default value for string fields.
|
static java.lang.String |
LOG_TAG |
protected android.content.Context |
m_ctx
The context in which preferences are managed.
|
protected android.content.SharedPreferences |
m_prefs
A persistent reference to the app's preferences.
|
protected int |
m_zDefaultInteger |
Constructor and Description |
---|
PreferencePortal(android.content.Context ctx) |
Modifier and Type | Method and Description |
---|---|
boolean |
getBoolean(int resKey) |
boolean |
getBoolean(int resKey,
boolean bDefault) |
boolean |
getBoolean(java.lang.String sKey) |
boolean |
getBoolean(java.lang.String sKey,
boolean bDefault) |
int |
getInt(int resKey) |
int |
getInt(int resKey,
int zDefault) |
int |
getInt(java.lang.String sKey) |
int |
getInt(java.lang.String sKey,
int zDefault) |
long |
getLong(int resKey) |
long |
getLong(int resKey,
long zDefault) |
long |
getLong(java.lang.String sKey) |
long |
getLong(java.lang.String sKey,
long zDefault) |
android.content.SharedPreferences |
getPrefs()
Accesses the
SharedPreferences instance used by this class. |
java.lang.String |
getString(int resKey) |
java.lang.String |
getString(int resKey,
java.lang.String sDefault) |
java.lang.String |
getString(java.lang.String sKey) |
java.lang.String |
getString(java.lang.String sKey,
java.lang.String sDefault) |
boolean |
getStringifiedBoolean(int resKey)
Fetches a Boolean preference that was stored as a string, using
DEFAULT_BOOLEAN as the default if the preference is not set. |
boolean |
getStringifiedBoolean(int resKey,
boolean bDefault)
Fetches a Boolean preference that was stored as a string.
|
static boolean |
getStringifiedBoolean(android.content.SharedPreferences prefs,
java.lang.String sKey,
boolean bDefault)
Get the value of a Boolean preference that was stored as a string.
|
boolean |
getStringifiedBoolean(java.lang.String sKey)
Fetches a Boolean preference that was stored as a string, using
DEFAULT_BOOLEAN as the default if the preference is not set. |
boolean |
getStringifiedBoolean(java.lang.String sKey,
boolean bDefault)
Fetches a Boolean preference that was stored as a string.
|
int |
getStringifiedInt(int resKey) |
int |
getStringifiedInt(int resKey,
int zDefault) |
static int |
getStringifiedInt(android.content.SharedPreferences prefs,
java.lang.String sKey,
int zDefault)
Get the value of an integer preference that is stored as a string.
|
int |
getStringifiedInt(java.lang.String sKey) |
int |
getStringifiedInt(java.lang.String sKey,
int zDefault) |
long |
getStringifiedLong(int resKey) |
long |
getStringifiedLong(int resKey,
long zDefault) |
static long |
getStringifiedLong(android.content.SharedPreferences prefs,
java.lang.String sKey,
long zDefault)
Get the value of a long-integer preference that is stored as a string.
|
long |
getStringifiedLong(java.lang.String sKey) |
long |
getStringifiedLong(java.lang.String sKey,
long zDefault) |
android.content.SharedPreferences.Editor |
putBoolean(int resKey,
boolean b) |
android.content.SharedPreferences.Editor |
putBoolean(java.lang.String sKey,
boolean b) |
android.content.SharedPreferences.Editor |
putInt(int resKey,
int z) |
android.content.SharedPreferences.Editor |
putInt(java.lang.String sKey,
int z) |
android.content.SharedPreferences.Editor |
putLong(int resKey,
long z) |
android.content.SharedPreferences.Editor |
putLong(java.lang.String sKey,
long z) |
android.content.SharedPreferences.Editor |
putString(int resKey,
java.lang.String s) |
android.content.SharedPreferences.Editor |
putString(java.lang.String sKey,
java.lang.String s) |
android.content.SharedPreferences.Editor |
putStringifiedBoolean(int resKey,
boolean b)
Edit the value of a Boolean preference that is stored as a string.
|
static android.content.SharedPreferences.Editor |
putStringifiedBoolean(android.content.SharedPreferences prefs,
java.lang.String sKey,
boolean b)
Edit the value of a Boolean preference that is stored as a string.
|
android.content.SharedPreferences.Editor |
putStringifiedBoolean(java.lang.String sKey,
boolean b)
Edit the value of a Boolean preference that is stored as a string.
|
android.content.SharedPreferences.Editor |
putStringifiedInt(int resKey,
int z) |
static android.content.SharedPreferences.Editor |
putStringifiedInt(android.content.SharedPreferences prefs,
java.lang.String sKey,
int z)
Edit the value of an integer preference that is stored as a string.
|
android.content.SharedPreferences.Editor |
putStringifiedInt(java.lang.String sKey,
int z) |
android.content.SharedPreferences.Editor |
putStringifiedLong(int resKey,
long z) |
static android.content.SharedPreferences.Editor |
putStringifiedLong(android.content.SharedPreferences prefs,
java.lang.String sKey,
long z)
Edit the value of a long-integer preference that is stored as a string.
|
android.content.SharedPreferences.Editor |
putStringifiedLong(java.lang.String sKey,
long z) |
PreferencePortal |
useIntegerDefaultValue(int zDefault)
Specifies the value that should be used as the integer default.
|
public static final java.lang.String LOG_TAG
public static final boolean DEFAULT_BOOLEAN
false
)public static final int DEFAULT_INT_NOT_SET
-1
). Assign this default when your integer preference is
intended to be unsigned, but knowing that the value has not been set at
all is valuable.public static final int DEFAULT_INT_ZERO
0
) The class will
choose this default value on its own, but it may be reconfigured with
useIntegerDefaultValue(int)
.public static final java.lang.String DEFAULT_STRING
null
protected int m_zDefaultInteger
protected android.content.Context m_ctx
protected android.content.SharedPreferences m_prefs
public static boolean getStringifiedBoolean(android.content.SharedPreferences prefs, java.lang.String sKey, boolean bDefault)
prefs
- the app's preferencessKey
- the preference keybDefault
- the default value, if not setpublic static android.content.SharedPreferences.Editor putStringifiedBoolean(android.content.SharedPreferences prefs, java.lang.String sKey, boolean b)
apply()
or
commit()
method of the preference editor to complete the change.prefs
- the app's preferencessKey
- the preference keyb
- the value to writeapply()
or commit()
public static int getStringifiedInt(android.content.SharedPreferences prefs, java.lang.String sKey, int zDefault) throws java.lang.NumberFormatException
prefs
- the app's preferencessKey
- the preference keyzDefault
- the default value, if not setjava.lang.NumberFormatException
- if the preference's value can't be parsedpublic static android.content.SharedPreferences.Editor putStringifiedInt(android.content.SharedPreferences prefs, java.lang.String sKey, int z)
apply()
or
commit()
method of the preference editor to complete the change.prefs
- the app's preferencessKey
- the preference keyz
- the value to writeapply()
or commit()
public static long getStringifiedLong(android.content.SharedPreferences prefs, java.lang.String sKey, long zDefault) throws java.lang.NumberFormatException
prefs
- the app's preferencessKey
- the preference keyzDefault
- the default value, if not setjava.lang.NumberFormatException
- if the preference's value can't be parsedpublic static android.content.SharedPreferences.Editor putStringifiedLong(android.content.SharedPreferences prefs, java.lang.String sKey, long z)
apply
or
commit()
method of the preference editor to complete the change.prefs
- the app's preferencessKey
- the preference keyz
- the value to writeapply()
or commit()
public android.content.SharedPreferences getPrefs()
SharedPreferences
instance used by this class.public PreferencePortal useIntegerDefaultValue(int zDefault)
zDefault
- a value to be used as the default for all integersDEFAULT_INT_ZERO
,
DEFAULT_INT_NOT_SET
public boolean getBoolean(java.lang.String sKey, boolean bDefault)
public boolean getBoolean(java.lang.String sKey)
public boolean getBoolean(int resKey, boolean bDefault)
public boolean getBoolean(int resKey)
public boolean getStringifiedBoolean(java.lang.String sKey, boolean bDefault)
sKey
- the preference keybDefault
- a default if not setpublic boolean getStringifiedBoolean(java.lang.String sKey)
DEFAULT_BOOLEAN
as the default if the preference is not set.sKey
- the preference keypublic boolean getStringifiedBoolean(int resKey, boolean bDefault)
resKey
- the ID of a string resource whose value is the preference
keybDefault
- a default if not setpublic boolean getStringifiedBoolean(int resKey)
DEFAULT_BOOLEAN
as the default if the preference is not set.resKey
- the ID of a string resource whose value is the preference
keypublic int getInt(java.lang.String sKey, int zDefault)
public int getInt(java.lang.String sKey)
public int getInt(int resKey, int zDefault)
public int getInt(int resKey)
public int getStringifiedInt(java.lang.String sKey, int zDefault) throws java.lang.NumberFormatException
java.lang.NumberFormatException
public int getStringifiedInt(java.lang.String sKey)
public int getStringifiedInt(int resKey, int zDefault)
public int getStringifiedInt(int resKey)
public long getLong(java.lang.String sKey, long zDefault)
public long getLong(java.lang.String sKey)
public long getLong(int resKey, long zDefault)
public long getLong(int resKey)
public long getStringifiedLong(java.lang.String sKey, long zDefault) throws java.lang.NumberFormatException
java.lang.NumberFormatException
public long getStringifiedLong(java.lang.String sKey) throws java.lang.NumberFormatException
java.lang.NumberFormatException
public long getStringifiedLong(int resKey, long zDefault) throws java.lang.NumberFormatException
java.lang.NumberFormatException
public long getStringifiedLong(int resKey) throws java.lang.NumberFormatException
java.lang.NumberFormatException
public java.lang.String getString(java.lang.String sKey, java.lang.String sDefault)
public java.lang.String getString(java.lang.String sKey)
public java.lang.String getString(int resKey, java.lang.String sDefault)
public java.lang.String getString(int resKey)
public android.content.SharedPreferences.Editor putBoolean(java.lang.String sKey, boolean b)
public android.content.SharedPreferences.Editor putBoolean(int resKey, boolean b)
public android.content.SharedPreferences.Editor putStringifiedBoolean(java.lang.String sKey, boolean b)
apply()
or
commit()
method of the preference editor to complete the change.sKey
- the preference keyb
- the value to writeapply()
or commit()
public android.content.SharedPreferences.Editor putStringifiedBoolean(int resKey, boolean b)
apply()
or
commit()
method of the preference editor to complete the change.resKey
- the ID of a string resource whose value is the preference
keyb
- the value to writeapply()
or commit()
public android.content.SharedPreferences.Editor putInt(java.lang.String sKey, int z)
public android.content.SharedPreferences.Editor putInt(int resKey, int z)
public android.content.SharedPreferences.Editor putStringifiedInt(java.lang.String sKey, int z)
public android.content.SharedPreferences.Editor putStringifiedInt(int resKey, int z)
public android.content.SharedPreferences.Editor putLong(java.lang.String sKey, long z)
public android.content.SharedPreferences.Editor putLong(int resKey, long z)
public android.content.SharedPreferences.Editor putStringifiedLong(java.lang.String sKey, long z)
public android.content.SharedPreferences.Editor putStringifiedLong(int resKey, long z)
public android.content.SharedPreferences.Editor putString(java.lang.String sKey, java.lang.String s)
public android.content.SharedPreferences.Editor putString(int resKey, java.lang.String s)