public interface SharedPreferences 
 
| android.content.SharedPreferences | 
用于访问和修改由getSharedPreferences(String, int)返回的偏好数据的getSharedPreferences(String, int) 。 对于任何特定的偏好设置,所有客户都共享此类的单个实例。 对首选项的修改必须经过一个SharedPreferences.Editor对象,以确保首选项值保存在一致的状态下,并在控制权存储时进行控制。 从各种get方法返回的对象必须被应用程序视为不可变的。
注意:这个类不支持跨多个进程使用。
有关使用SharedPreferences的更多信息,请阅读 Data Storage开发人员指南。
| Nested classes | |
|---|---|
|  interface | SharedPreferences.Editor用于修改 | 
|  interface | SharedPreferences.OnSharedPreferenceChangeListener在共享首选项发生更改时调用回调的接口定义。 | 
| 公共方法(Public methods) | |
|---|---|
|  abstract boolean |  contains(String key) 检查首选项是否包含首选项。 | 
|  abstract SharedPreferences.Editor |  edit() 为这些首选项创建一个新的编辑器,通过它可以对首选项中的数据进行修改,并自动将这些更改返回给SharedPreferences对象。 | 
|  abstract Map<String, ?> |  getAll() 从首选项中检索所有值。 | 
|  abstract boolean |  getBoolean(String key, boolean defValue) 从首选项中检索一个布尔值。 | 
|  abstract float |  getFloat(String key, float defValue) 从首选项中检索一个浮点值。 | 
|  abstract int |  getInt(String key, int defValue) 从首选项中检索一个int值。 | 
|  abstract long |  getLong(String key, long defValue) 从首选项中检索一个长整型值。 | 
|  abstract String |  getString(String key, String defValue) 从首选项中检索字符串值。 | 
|  abstract Set<String> |  getStringSet(String key, Set<String> defValues) 从首选项中检索一组字符串值。 | 
|  abstract void |  registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) 注册要在首选项发生更改时调用的回调。 | 
|  abstract void |  unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) 取消注册以前的回叫。 | 
boolean contains (String key)
检查首选项是否包含首选项。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to check. | 
| 返回(Returns) | |
|---|---|
| boolean | Returns true if the preference exists in the preferences, otherwise false. | 
SharedPreferences.Editor edit ()
为这些首选项创建一个新的编辑器,通过它可以对首选项中的数据进行修改,并自动将这些更改返回给SharedPreferences对象。
请注意,您 必须调用 commit()才能在编辑器中执行的任何更改实际显示在SharedPreferences中。
| 返回(Returns) | |
|---|---|
| SharedPreferences.Editor | Returns a new instance of the SharedPreferences.Editorinterface, allowing you to modify the values in this SharedPreferences object. | 
Map<String, ?> getAll ()
从首选项中检索所有值。
请注意,您不得修改此方法返回的集合,也不得修改其中的任何内容。 如果你这样做,你的存储数据的一致性是不能保证的。
| 返回(Returns) | |
|---|---|
| Map<String, ?> | Returns a map containing a list of pairs key/value representing the preferences. | 
| 抛出异常(Throws) | |
|---|---|
| NullPointerException | |
boolean getBoolean (String key, boolean defValue)
从首选项中检索一个布尔值。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to retrieve. | 
| defValue | boolean: Value to return if this preference does not exist. | 
| 返回(Returns) | |
|---|---|
| boolean | 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 | |
float getFloat (String key, float defValue)
从首选项中检索一个浮点值。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to retrieve. | 
| defValue | float: Value to return if this preference does not exist. | 
| 返回(Returns) | |
|---|---|
| float | 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 | |
int getInt (String key, int defValue)
从首选项中检索一个int值。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to retrieve. | 
| defValue | int: Value to return if this preference does not exist. | 
| 返回(Returns) | |
|---|---|
| int | 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 | |
long getLong (String key, long defValue)
从首选项中检索一个长整型值。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to retrieve. | 
| defValue | long: Value to return if this preference does not exist. | 
| 返回(Returns) | |
|---|---|
| long | 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 | |
String getString (String key, String defValue)
从首选项中检索字符串值。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to retrieve. | 
| defValue | String: Value to return if this preference does not exist. | 
| 返回(Returns) | |
|---|---|
| String | 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 | |
Set<String> getStringSet (String key, Set<String> defValues)
从首选项中检索一组字符串值。
请注意,您不得修改此调用返回的设置实例。 如果存储数据的一致性无法保证,您也无法修改该实例。
| 参数(Parameters) | |
|---|---|
| key | String: The name of the preference to retrieve. | 
| defValues | Set: Values to return if this preference does not exist. | 
| 返回(Returns) | |
|---|---|
| Set<String> | 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 | |
void registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
注册要在首选项发生更改时调用的回调。
警告:首选项管理器当前不存储对侦听器的强引用。 您必须存储对侦听器的强引用,否则它将容易被垃圾收集。 只要您需要侦听器,我们建议您在对象的实例数据中保留对侦听器的引用。
| 参数(Parameters) | |
|---|---|
| listener | SharedPreferences.OnSharedPreferenceChangeListener: The callback that will run. | 
void unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
取消注册以前的回叫。
| 参数(Parameters) | |
|---|---|
| listener | SharedPreferences.OnSharedPreferenceChangeListener: The callback that should be unregistered. |