Reading Preferences
By default, the all your app ' s preferences is saved to a file that's accessible from anywhere within your application by call ing the static method PreferenceManager.getDefaultSharedPreferences() . This returns the SharedPreferences object containing all the key-value pairs that is associated with the Preference objects used in your .
For example, here's how can I read one of the preference values from a and activity in your application:
Sharedpreferences=preferencemanager. Getdefaultsharedpreferences(this);
String= sharedpref. GetString(settingsactivity. Key_pref_sync_conn,"");
Listening for preference changes
There is several reasons you might want to be notified as soon as the user changes one of the preferences. In order to receive a callback if a change happens to any one of the preferences, implement the SharedPreference.OnSharedPreferenceChangeListener interface and Regis ter the listener for the SharedPreferences object by calling registerOnSharedPreferenceChangeListener() .
The interface have only one callback method, onSharedPreferenceChanged() and you might find it easiest to implement the interface as a part of Y Our activity. For example:
Public classSettingsactivityextendspreferenceactivityImplementsOnsharedpreferencechangelistener { Public Static FinalString key_pref_sync_conn = "Pref_syncconnectiontype"; ... Public voidonsharedpreferencechanged (sharedpreferences sharedpreferences, String key) {if(Key.equals (key_pref_sync_conn)) {Preference Connectionpref=findpreference (key); //Set Summary to is the user-description for the selected valueConnectionpref.setsummary (sharedpreferences.getstring (Key, "")); } }}
In this example, the method checks whether the changed setting are for a known preference key. It calls to get the object that is findPreference() Preference changed so it can modify the item's summary to be a description of the user ' s selection. That's, when the setting was a ListPreference or other multiple choice setting, and you should call when the setSummary() setting changes to D Isplay the current status (such as the Sleep setting shown in Figure 5).
Note: As described in the "Android Design document" About Settings, we recommend this update the summary for a each time ListPreference The user changes the preference in order to describe the current setting.
For proper lifecycle management in the activity, we recommend so you register and unregister SharedPreferences.OnSharedPreferenceChangeListener onResume() your during the onPause()and callbacks, respectively:
@Override protected void Onresume () { super. Onresume (); Getpreferencescreen (). Getsharedpreferences () . Registeronsharedpreferencechangelistener (this );} @Overrideprotectedvoid onPause () { super. OnPause (); Getpreferencescreen (). Getsharedpreferences () . Unregisteronsharedpreferencechangelistener (this );}
Caution: When you call registerOnSharedPreferenceChangeListener() , the preference manager does not currently store a strong reference to the listener. You must store a strong reference to the listener, or it'll be susceptible to garbage collection. We recommend keep a reference to the listener in the instance data of an object that would exist as long as you need th E listener.
For example, with the following code, the caller does not keep a reference to the listener. As a result, the listener is subject to garbage collection, and it'll fail at some indeterminate time :
Don't use it as follows
Prefs.registeronsharedpreferencechangelistener (//bad! The listener is subject to garbage collection! NewSharedpreferences.onsharedpreferencechangelistener () { Public voidonsharedpreferencechanged (sharedpreferences prefs, String key) {//Listener Implementation }});
Instead, store a reference to the listener in an instance data field of an object that would exist as long as the listener is needed:
Sharedpreferences.onsharedpreferencechangelistener listener = new Sharedpreferences.onsharedpreferencechangelistener () { publicvoid Onsharedpreferencechanged (Sharedpreferences prefs, String key) { // Listener implementation }};p Refs.registeronsharedpreferencechangelistener (listener);
Android Preferences (6) apply and listen to each preference parameter