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, setString
public 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 TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public double getDouble(String key, double defaultValue)
TypedProperties
key
is null or there is no property for the key,
defaultValue
is returned.getDouble
in class TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public void setDouble(String key, double value)
TypedProperties
key
is null, nothing is done.setDouble
in class TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public 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 TypedProperties
public void clear()
edit().clear().commit()
on the internal SharedPreferences instance.clear
in class TypedProperties
Copyright © 2016. All rights reserved.