Java Reference
In-Depth Information
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
The code begins with a TimerTask subclass that notifies the MIDlet that the alarm
fired; this subclass needs two methods: setOwner , which hooks the task to the MIDlet, and
run , which performs the action when the timer invokes the task.
The initialize method, which startApp invokes when the AMS launches the MIDlet
for any reason, must do two things. First, it must determine whether the MIDlet is launch-
ing because of an alarm invocation or a user invocation, and second, it must display the
appropriate screen (either an informative form or the alarm alert) depending on whether
the alarm fired while the application was not running. To do this, the MIDlet must persist
the desired alarm time so that it can tell the difference between a user-initiated launch
and an alarm launch. To persist this data, the MIDlet uses the Java ME record store, which
I discuss in more detail in Chapter 6. For now, it suffices to know that the record store
persists records that are arrays of bytes; I use instances of the ByteArrayInputStream and
DataInputStream classes to decode the desired alarm time if the MIDlet detects a record in
the store. (The scheduleMIDlet method, which I discuss next, is responsible for writing this
record upon exit if an alarm is set.) If the alarm time was before the current time, the
MIDlet will show the alarm alert by setting it to the current displayable item. If no record
was found, or if the alarm time hasn't been reached, the MIDlet will create a new instance
of MyTask and schedule its task to fire at the appropriate time.
The scheduleMIDlet method does the opposite. Invoked upon the MIDlet exit, it
checks the current time against the launch time, and if it is before the time for the alarm
to fire, it will register the alarm with the push registry before writing the alarm time to the
persistent record store using instances of ByteArrayOutputStream and DataOutputStream .
The exitMIDlet method invokes scheduleMIDlet when you exit the application through the
exit command (so the alarm is only scheduled on normal exits, not on AMS-induced ter-
mination).
Both the timer and the alarm use the alarmFired method, which simply sets the cur-
rent display to be the alarm alert notification instead of the default informative screen
the MIDlet shows at application launch.
The remainder of the code initializes the user interface:
get_infoForm : This method lazily creates an instance of the form shown at applica-
tion launch.
get_helloStringItem : This method lazily creates an instance of the string item used
by the introductory form.
 
Search WWH ::




Custom Search