public void setValue (String key, String value) { <-' methods if (_editor == null) return; _editor.putString(key,value) ;

public String getEmail(){ <i—© Extract email value if (_prefs == null) return "Unknown";

_useremailaddress = _prefs.getString("emailaddress","Unknown"); return _useremailaddress;

public void setEmail (String newemail) { <1—© Set email value if (_editor == null) return;

_editor.putString("emailaddress",newemail) ;

... (abbreviated for brevity) public void save () { <—© Save preferences if (_editor == null) return; _editor.commit();

To persist the application's settings data, we employ a SharedPreferences object O. To manipulate data within the SharedPreferences object, here named simply _prefs, we use an instance of the Editor class ©. This snippet employs some default settings values ©, which are appropriate for our application. The Prefs() constructor © does the necessary housekeeping so we can establish our private SharedPreferences object, including using a passed-in Context instance. The Context class is necessary because the SharedPreferences mechanism relies on a Context for segregating data. This snippet shows a pair of set and get methods that are generic in nature ©. The getEmail © and setEmail methods © are responsible for manipulating the email setting value. The save() method © invokes a commit() on the Editor, which persists the data to the SharedPreferences store.

Now that you have some feel for how this important preference data is stored, let's return to examine the code of

0 0

Post a comment