FileDocCategorySizeDatePackage
SharedPreferences.javaAPI DocAndroid 5.1 API15300Thu Mar 12 22:22:10 GMT 2015android.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. 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.

Developer Guides

For more information about using SharedPreferences, read the Data Storage developer guide.

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.

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.

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 java.util.SetgetStringSet(java.lang.String key, java.util.Set defValues)
Retrieve a set of String values from the preferences.

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.

param
key The name of the preference to retrieve.
param
defValues Values to return if this preference does not exist.
return
Returns the preference values if they exist, or defValues. Throws ClassCastException if there is a preference with this name that is not a Set.
throws
ClassCastException

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

Caution: The preference manager does not currently store a strong reference to the listener. You must store a strong reference to the listener, or it will be susceptible to garbage collection. We recommend you keep a reference to the listener in the instance data of an object that will exist as long as you need the listener.

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