Interface for accessing and modifying preference data returned by getSharedPreferences(String, int)
. For any particular set of preferences,
获得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.
修改的时候 我们需要Editor
getSharedPreferences(String, int)
Interface used for modifying values in a SharedPreferences
object.
SharedPreferences.OnSharedPreferenceChangeListener
Interface definition for a callback to be invoked when a shared preference is changed.
SharedPreferences.Editor
Interface used for modifying values in aSharedPreferences
object. All changes you make in an editor are batched, and not copied back to the original
SharedPreferences
until you call
commit()
or
apply()
public abstract SharedPreferences.Editor clear ()
Mark in the editor to remove all values from the preferences. Once commit is called, the only remaining preferences will be any that you have defined in this editor.
Note that when committing back to the preferences, the clear is done first, regardless of whether you called clear before or after put methods on this editor.
Returns
- Returns a reference to the same Editor object, so you can chain put calls together.
public abstract boolean commit ()
Commit your preferences changes back from this Editor to the SharedPreferences
object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.
Note that when two editors are modifying preferences at the same time, the last one to call commit wins.
If you don't care about the return value and you're using this from your application's main thread, consider using apply()
instead.
Returns
- Returns true if the new values were successfully written to persistent storage.
public abstract SharedPreferences.Editor putBoolean (String key, boolean value)
public abstract SharedPreferences.Editor putFloat (String key, float value)
public abstract SharedPreferences.Editor putInt (String key, int value)
public abstract SharedPreferences.Editor putLong (String key, long value)
public abstract SharedPreferences.Editor putString (String key, String value)
Set a String value in the preferences editor, to be written back once commit()
or apply()
are called.
Parameters
key | The name of the preference to modify. |
---|---|
value | The new value for the preference. Supplying null as the value is equivalent to calling remove(String) with this key. |
Returns
- Returns a reference to the same Editor object, so you can chain put calls together.
public abstract SharedPreferences.Editor remove (String key)
Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit()
is called.
Note that when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor.
Parameters
key | The name of the preference to remove. |
---|
Returns
- Returns a reference to the same Editor object, so you can chain put calls together.
Sharedpreferences
public abstract 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.
public abstract 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 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.
public abstract 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 |
---|
public abstract 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 |
---|
public abstract 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 |
---|
public abstract 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 |
---|
public abstract 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 |
---|
注意 这个注册 数据监听器 是注册在SharedPreferences上面的。
public abstract 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. |
---|
public abstract void unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
Unregisters a previous callback.
Parameters
listener | The callback that should be unregistered. |
---|
取消注册。
例子:
注意 这个地方也给我们一种很好的提示 如果需要使用 Context 上下文的时候 我们可以再函数里面 直接创建一个
Context context 从而就可以直接使用context上下文对象。
public static boolean saveFile(Context context, String username,
String password)
{
// 获取SP对象
SharedPreferences sp = context.getSharedPreferences("info",
Context.MODE_PRIVATE);
// 获取editor对象。
Editor editor = sp.edit();
// 放入数据
editor.putString("username", username);
editor.putString("password", password);
// 提交
editor.commit();
return true;
}