public interface SharedPreferences
Context.getSharedPreferences(java.lang.String, int)
. For any particular set of preferences,
there is a single instance of this class that all clients share.
Modifications to the preferences must go through an SharedPreferences.Editor
object
to ensure the preference values remain in a consistent state and control
when they are committed to storage. Objects that are returned from the
various get
methods must be treated as immutable by the application.
Note: currently this class does not support use across multiple processes. This will be added later.
For more information about using SharedPreferences, read the Data Storage developer guide.
Modifier and Type | Interface and Description |
---|---|
static interface |
SharedPreferences.Editor
Interface used for modifying values in a
SharedPreferences
object. |
static interface |
SharedPreferences.OnSharedPreferenceChangeListener
Interface definition for a callback to be invoked when a shared
preference is changed.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(String key)
Checks whether the preferences contains a preference.
|
SharedPreferences.Editor |
edit()
Create a new Editor for these preferences, through which you can make
modifications to the data in the preferences and atomically commit those
changes back to the SharedPreferences object.
|
Map<String,?> |
getAll()
Retrieve all values from the preferences.
|
boolean |
getBoolean(String key,
boolean defValue)
Retrieve a boolean value from the preferences.
|
float |
getFloat(String key,
float defValue)
Retrieve a float value from the preferences.
|
int |
getInt(String key,
int defValue)
Retrieve an int value from the preferences.
|
long |
getLong(String key,
long defValue)
Retrieve a long value from the preferences.
|
String |
getString(String key,
String defValue)
Retrieve a String value from the preferences.
|
Set<String> |
getStringSet(String key,
Set<String> defValues)
Retrieve a set of String values from the preferences.
|
void |
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
Registers a callback to be invoked when a change happens to a preference.
|
void |
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
Unregisters a previous callback.
|
Map<String,?> getAll()
Note that you must not modify the collection returned by this method, or alter any of its contents. The consistency of your stored data is not guaranteed if you do.
NullPointerException
String getString(String key, String defValue)
key
- The name of the preference to retrieve.defValue
- Value to return if this preference does not exist.ClassCastException
Set<String> getStringSet(String key, Set<String> defValues)
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.
key
- The name of the preference to retrieve.defValues
- Values to return if this preference does not exist.ClassCastException
int getInt(String key, int defValue)
key
- The name of the preference to retrieve.defValue
- Value to return if this preference does not exist.ClassCastException
long getLong(String key, long defValue)
key
- The name of the preference to retrieve.defValue
- Value to return if this preference does not exist.ClassCastException
float getFloat(String key, float defValue)
key
- The name of the preference to retrieve.defValue
- Value to return if this preference does not exist.ClassCastException
boolean getBoolean(String key, boolean defValue)
key
- The name of the preference to retrieve.defValue
- Value to return if this preference does not exist.ClassCastException
boolean contains(String key)
key
- The name of the preference to check.SharedPreferences.Editor edit()
Note that you must call SharedPreferences.Editor.commit()
to have any
changes you perform in the Editor actually show up in the
SharedPreferences.
SharedPreferences.Editor
interface, allowing
you to modify the values in this SharedPreferences object.void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
listener
- The callback that will run.unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)
void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
listener
- The callback that should be unregistered.registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)