Preferences

Press the button to toggle between date and time. The current preference will be saved. See Shared Preferences. Saving Persistent State says that an Activity should save its preferences in the Activity’s onPause method.

Source code in Preferences.zip

  1. PreferencesActivity.java
  2. R.java
  3. main.xml

Examine the preferencesFilename.xml file

We got the mode bits rw-rw---- because we created the file with MODE_PRIVATE.

adb shell
# cd /data/data/edu.nyu.scps.preferences/shared_prefs
# pwd
# ls -l
-rw-rw---- app_34   app_34        112 2011-05-04 10:38 preferencesFilename.xml
# cat preferencesFilename.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="showDate" value="false" />
</map>
# exit

Get a copy of preferencesFilename.xml

# cd
# adb pull /data/data/edu.nyu.scps.preferences/shared_prefs/preferencesFilename.xml
5 KB/s (111 bytes in 0.019s)
# exit
ls -l preferencesFilename.xml
-rw-r--r--  1 myname  staff  111 May  4 11:07 preferencesFilename.xml

ApiDemos/Apps/Preferences

  1. Preferences from XML. An Activity that extends PreferenceActivity and reads the ptreferences from preferences.xml. #5 is Prefercnes from code; it looks exactly the same.
  2. Launching preferences. An Activity that uses an Intent object to launch an Activity that extends PreferenceActivity. It receives the result in onActivityResult.