Saving and Restoring Persistent User Preferences in MIDlets Tech Tip on Using Record Management System Classes
Eric Giguere demonstrates how to store persistent user preferences in a
MIDlet (mobile applications based on the Mobile Information Device
Profile -- MIDP) as a single record rather than as several individual
records. These preferences can be anything from an email address to
information relating to recent uses of the application, such as which
screen was last displayed. The Tech Tip includes examples of code.
Giguere recommends using the Singleton design pattern to ensure that
preferences are read and written consistently throughout the
application. The Prefs class must be coded so that the MIDlet can have
only one instance of it. The MIDlet will call either its
single-instance getInstance method or MIDlet.getAppProperty to get the
application descriptor properties, which is a convenient source of
default values for user prefererences.
With the Singleton obtained, the MIDlet can use one of three get
methods to obtain a property value: getStringProperty(),
getBooleanProperty() or getIntProperty(). Each method takes two
parameters: the property name and the default value to return if the
property is undefined.
To change the value of a property, the MIDlet calls
setStringProperty(), setBooleanProperty() or setIntProperty(). The
property is stored internally in the Prefs object, but not immediately
written to the record store. Because writing to the record store can be
a slow operation on some devices, Prefs does so only when its save()
method is called.
With a simple MIDlet, Giguere shows how to use the Prefs class. The MIDlet
allows users to test it, set appropriate values, exit, restart the application
and verify that the desired preferences were saved.
Additional information on record stores is available in the JavaTM 2
Platform, Micro Edition (J2METM) Technical Tips, which include Record
Management System Basics and Using Record Stores Efficiently.
Customized news reports about Sun Microsystems. Just the news you need, none of what you don't. 50,000+ Members. 20,000+ Articles Published since 1998.