Build 1.0_r1(from source)

android.content
Interface SharedPreferences


public interface SharedPreferences

Interface for accessing and modifying preference data returned by 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.

Note: currently this class does not support use across multiple processes. This will be added later.

See Also:
Context.getSharedPreferences(java.lang.String, int)

Nested Class Summary
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.
 
Method Summary
 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.
 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.
 

Method Detail

getAll

Map<String,?> getAll()
Retrieve all values from the preferences.

Returns:
Returns a map containing a list of pairs key/value representing the preferences.
Throws:
NullPointerException

getString

String getString(String key,
                 String defValue)
Retrieve a String value from the preferences.

Parameters:
key - The name of the preference to retrieve.
defValue - Value to return if this preference does not exist.
Returns:
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.
Throws:
ClassCastException

getInt

int getInt(String key,
           int defValue)
Retrieve an int value from the preferences.

Parameters:
key - The name of the preference to retrieve.
defValue - Value to return if this preference does not exist.
Returns:
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not an int.
Throws:
ClassCastException

getLong

long getLong(String key,
             long defValue)
Retrieve a long value from the preferences.

Parameters:
key - The name of the preference to retrieve.
defValue - Value to return if this preference does not exist.
Returns:
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a long.
Throws:
ClassCastException

getFloat

float getFloat(String key,
               float defValue)
Retrieve a float value from the preferences.

Parameters:
key - The name of the preference to retrieve.
defValue - Value to return if this preference does not exist.
Returns:
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a float.
Throws:
ClassCastException

getBoolean

boolean getBoolean(String key,
                   boolean defValue)
Retrieve a boolean value from the preferences.

Parameters:
key - The name of the preference to retrieve.
defValue - Value to return if this preference does not exist.
Returns:
Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a boolean.
Throws:
ClassCastException

contains

boolean contains(String key)
Checks whether the preferences contains a preference.

Parameters:
key - The name of the preference to check.
Returns:
Returns true if the preference exists in the preferences, otherwise false.

edit

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.

Note that you must call SharedPreferences.Editor.commit() to have any changes you perform in the Editor actually show up in the SharedPreferences.

Returns:
Returns a new instance of the SharedPreferences.Editor interface, allowing you to modify the values in this SharedPreferences object.

registerOnSharedPreferenceChangeListener

void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
Registers a callback to be invoked when a change happens to a preference.

Parameters:
listener - The callback that will run.
See Also:
unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)

unregisterOnSharedPreferenceChangeListener

void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
Unregisters a previous callback.

Parameters:
listener - The callback that should be unregistered.
See Also:
registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener)

Build 1.0_r1(from source)

Please submit a feedback, bug or feature