1. Save Data
SharedPreferences shprefs
= getSharedPreferences((String)"GroupSMSApp", MODE_PRIVATE);
SharedPreferences.Editor preeditor = shprefs.edit();
preeditor.putString((String)"s_prefix", mPrefix);
preeditor.putString((String)"s_postfix", mPostfix);
preeditor.putString((String)"s_message", mMessage);
preeditor.commit();
2. Getting Data
SharedPreferences prefs =
getApplicationContext().getSharedPreferences((String)"GroupSMSApp", 0);
String mStrEditPrefix = prefs.getString((String)"s_prefix", "");
if(mStrEditPrefix != null)
mEditPrefix.setText(mStrEditPrefix);
String mStrEditPostfix = prefs.getString((String)"s_postfix", "");
if(mStrEditPostfix != null)
mEditPostfix.setText(mStrEditPostfix);
String mStrEditMessage = prefs.getString((String)"s_message", "");
if(mStrEditMessage != null)
mEditMessage.setText(mStrEditMessage);
3. Saved Location
data/data/PACAKGE_NAME/shared_prefs/GroupSMSApp.xml
From : http://developer.android.com
public interface
SharedPreferences
android.content.SharedPreferences |
Class Overview
Interface for accessing and modifying preference data returned by
getSharedPreferences(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. 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 Also
Summary
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
SharedPreferences.Editor | 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. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Checks whether the preferences contains a preference.
| |||||||||||
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.
| |||||||||||
Retrieve all values from the preferences.
| |||||||||||
Retrieve a boolean value from the preferences.
| |||||||||||
Retrieve a float value from the preferences.
| |||||||||||
Retrieve an int value from the preferences.
| |||||||||||
Retrieve a long value from the preferences.
| |||||||||||
Retrieve a String value from the preferences.
| |||||||||||
Retrieve a set of String values from the preferences.
| |||||||||||
Registers a callback to be invoked when a change happens to a preference.
| |||||||||||
Unregisters a previous callback.
|
Public Methods
public abstract boolean contains (String key)
Added in API level 1
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 ()
Added in API level 1
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 Map<String, ?> getAll ()
Added in API level 1
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.
Returns
- Returns a map containing a list of pairs key/value representing the preferences.
Throws
NullPointerException |
---|
public abstract boolean getBoolean (String key, boolean defValue)
Added in API level 1
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)
Added in API level 1
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)
Added in API level 1
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)
Added in API level 1
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)
Added in API level 1
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 |
---|
public abstract Set<String> getStringSet (String key, Set<String> defValues)
Added in API level 11
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.
Parameters
key | The name of the preference to retrieve. |
---|---|
defValues | Values to return if this preference does not exist. |
Returns
- 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 abstract void registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
Added in API level 1
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)
Added in API level 1
Unregisters a previous callback.
Parameters
listener | The callback that should be unregistered. |
---|
No comments:
Post a Comment