FileDocCategorySizeDatePackage
SharedPreferences.javaAPI DocAndroid 1.5 API10506Wed May 06 22:41:54 BST 2009android.content

SharedPreferences

public interface SharedPreferences
Interface for accessing and modifying preference data returned by {@link Context#getSharedPreferences}. 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 {@link 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
Context#getSharedPreferences

Fields Summary
Constructors Summary
Methods Summary
public booleancontains(java.lang.String key)
Checks whether the preferences contains a preference.

param
key The name of the preference to check.
return
Returns true if the preference exists in the preferences, otherwise false.

public android.content.SharedPreferences$Editoredit()
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 {@link Editor#commit} to have any changes you perform in the Editor actually show up in the SharedPreferences.

return
Returns a new instance of the {@link Editor} interface, allowing you to modify the values in this SharedPreferences object.

public java.util.MapgetAll()
Retrieve all values from the preferences.

return
Returns a map containing a list of pairs key/value representing the preferences.
throws
NullPointerException

public booleangetBoolean(java.lang.String key, boolean defValue)
Retrieve a boolean value from the preferences.

param
key The name of the preference to retrieve.
param
defValue Value to return if this preference does not exist.
return
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

public floatgetFloat(java.lang.String key, float defValue)
Retrieve a float value from the preferences.

param
key The name of the preference to retrieve.
param
defValue Value to return if this preference does not exist.
return
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

public intgetInt(java.lang.String key, int defValue)
Retrieve an int value from the preferences.

param
key The name of the preference to retrieve.
param
defValue Value to return if this preference does not exist.
return
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

public longgetLong(java.lang.String key, long defValue)
Retrieve a long value from the preferences.

param
key The name of the preference to retrieve.
param
defValue Value to return if this preference does not exist.
return
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

public java.lang.StringgetString(java.lang.String key, java.lang.String defValue)
Retrieve a String value from the preferences.

param
key The name of the preference to retrieve.
param
defValue Value to return if this preference does not exist.
return
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

public voidregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences$OnSharedPreferenceChangeListener listener)
Registers a callback to be invoked when a change happens to a preference.

param
listener The callback that will run.
see
#unregisterOnSharedPreferenceChangeListener

public voidunregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences$OnSharedPreferenceChangeListener listener)
Unregisters a previous callback.

param
listener The callback that should be unregistered.
see
#registerOnSharedPreferenceChangeListener