public class Preferences extends TypedProperties
As getXxx() and setXxx() methods accept Enum<?> as key as well as String, a code snippet like below
private static final String KEY_AAA = "AAA"; private static final String KEY_BBB = "BBB"; SharedPreferences preferences = ... String aaa = preferences.getString(KEY_AAA, null); String bbb = preferences.getString(KEY_BBB, null);
can be written like below.
enum Key
{
AAA,
BBB
;
}
Preferences preferences = ...
String aaa = preferences.getString(Key.AAA);
String bbb = preferences.getString(Key.BBB);
setXxx(key, value) methods are equivalent to edit().putXxx(key, value).commit().
App.getInstance().getPreferences() returns a Preferences instance that is supposed to be
used as application-wide preferences.
| Constructor and Description |
|---|
Preferences(android.content.Context context,
String name)
Equivalent to
this(context, name, Context.MODE_PRIVATE). |
Preferences(android.content.Context context,
String name,
int mode)
Constructor with parameters to get SharedPreferences.
|
Preferences(android.content.SharedPreferences prefs)
Constructor with a wrapped
SharedPreferences. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Equivalent to
edit().clear().commit()
on the internal SharedPreferences instance. |
boolean |
contains(String key)
Equivalent to
contains(key) on the internal
SharedPreferences instance. |
boolean |
getBoolean(String key,
boolean defaultValue)
Equivalent to
getBoolean(key, defaultValue)
on the internal SharedPreferences instance. |
double |
getDouble(String key,
double defaultValue)
Get the value of the property identified by the key as double.
|
float |
getFloat(String key,
float defaultValue)
Equivalent to
getFloat(key, defaultValue)
on the internal SharedPreferences instance. |
int |
getInt(String key,
int defaultValue)
Equivalent to
getInt(key, defaultValue)
on the internal SharedPreferences instance. |
long |
getLong(String key,
long defaultValue)
Equivalent to
getLong(key, defaultValue)
on the internal SharedPreferences instance. |
String |
getString(String key,
String defaultValue)
Equivalent to
getString(key, defaultValue)
on the internal SharedPreferences instance. |
void |
remove(String key)
Equivalent to
edit().remove(key).commit()
on the internal SharedPreferences instance. |
void |
setBoolean(String key,
boolean value)
Equivalent to
edit().putBoolean(key, value).commit()
on the internal SharedPreferences instance. |
void |
setDouble(String key,
double value)
Set the value to the property identified by the key.
|
void |
setFloat(String key,
float value)
Equivalent to
edit().putFloat(key, value).commit()
on the internal SharedPreferences instance. |
void |
setInt(String key,
int value)
Equivalent to
edit().putInt(key, value).commit()
on the internal SharedPreferences instance. |
void |
setLong(String key,
long value)
Equivalent to
edit().putLong(key, value).commit()
on the internal SharedPreferences instance. |
void |
setString(String key,
String value)
Equivalent to
edit().putString(key, value).commit()
on the internal SharedPreferences instance. |
contains, get, get, get, get, get, get, get, get, get, get, get, get, get, get, get, get, get, get, getBoolean, getBoolean, getBoolean, getDouble, getDouble, getDouble, getEnum, getEnum, getEnum, getEnum, getEnum, getEnum, getFloat, getFloat, getFloat, getInt, getInt, getInt, getLong, getLong, getLong, getString, getString, getString, remove, set, set, set, set, set, set, set, set, set, set, set, set, set, set, setBoolean, setDouble, setEnum, setEnum, setFloat, setInt, setLong, setStringpublic Preferences(android.content.Context context,
String name)
this(context, name, Context.MODE_PRIVATE).public Preferences(android.content.Context context,
String name,
int mode)
context - name - Preference name. This argument is given as the
first argument to
Context.getSharedPreferences(String, int).mode - Operating mode. This argument is given as the
second argument to
Context.getSharedPreferences(String, int).public Preferences(android.content.SharedPreferences prefs)
SharedPreferences.prefs - An SharedPreferences to be wrapped.public boolean contains(String key)
contains(key) on the internal
SharedPreferences instance.contains in class TypedPropertiespublic boolean getBoolean(String key, boolean defaultValue)
getBoolean(key, defaultValue)
on the internal SharedPreferences instance.
If the given key is null, defaultValue is returned.getBoolean in class TypedPropertiespublic float getFloat(String key, float defaultValue)
getFloat(key, defaultValue)
on the internal SharedPreferences instance.
If the given key is null, defaultValue is returned.getFloat in class TypedPropertiespublic double getDouble(String key, double defaultValue)
TypedPropertieskey is null or there is no property for the key,
defaultValue is returned.getDouble in class TypedPropertiespublic int getInt(String key, int defaultValue)
getInt(key, defaultValue)
on the internal SharedPreferences instance.
If the given key is null, defaultValue is returned.getInt in class TypedPropertiespublic long getLong(String key, long defaultValue)
getLong(key, defaultValue)
on the internal SharedPreferences instance.
If the given key is null, defaultValue is returned.getLong in class TypedPropertiespublic String getString(String key, String defaultValue)
getString(key, defaultValue)
on the internal SharedPreferences instance.
If the given key is null, defaultValue is returned.getString in class TypedPropertiespublic void setBoolean(String key, boolean value)
edit().putBoolean(key, value).commit()
on the internal SharedPreferences instance.
If the given key is null, nothing is done.setBoolean in class TypedPropertiespublic void setFloat(String key, float value)
edit().putFloat(key, value).commit()
on the internal SharedPreferences instance.
If the given key is null, nothing is done.setFloat in class TypedPropertiespublic void setDouble(String key, double value)
TypedPropertieskey is null, nothing is done.setDouble in class TypedPropertiespublic void setInt(String key, int value)
edit().putInt(key, value).commit()
on the internal SharedPreferences instance.
If the given key is null, nothing is done.setInt in class TypedPropertiespublic void setLong(String key, long value)
edit().putLong(key, value).commit()
on the internal SharedPreferences instance.
If the given key is null, nothing is done.setLong in class TypedPropertiespublic void setString(String key, String value)
edit().putString(key, value).commit()
on the internal SharedPreferences instance.
If the given key is null, nothing is done.setString in class TypedPropertiespublic void remove(String key)
edit().remove(key).commit()
on the internal SharedPreferences instance.
If the given key is null, nothing is done.remove in class TypedPropertiespublic void clear()
edit().clear().commit()
on the internal SharedPreferences instance.clear in class TypedPropertiesCopyright © 2016. All rights reserved.