To illustrate the MIDlet lifecycle and how it can be controlled, we’ll create a very simple MIDlet that does the following:
Prints a message when its constructor is called.
Creates a timer that fires from time to time, putting the MIDlet in the paused state if it is active and returning it to the active state if it is paused. When the timer has been through this cycle twice, it terminates the MIDlet.
Creates a background thread when it is started that simply prints a message every second. This thread is allowed to run only when the MIDlet is active.
Since you haven’t yet seen how to create user interfaces, this example MIDlet communicates by writing messages to its standard output stream. On a real device, you can’t see what is written to standard output or standard error (unless you are using debug facilities provided by the device vendor), but most device emulators provide a way to monitor the content of these streams. There are several products available that allow you to build and test MIDlets either in an emulated environment or on the real device; some of these products are described in Chapter 9. Here, we’ll use the Wireless Toolkit, which is available free of charge from Sun.
The Wireless Toolkit provides an implementation of MIDP together with an emulator that can be customized to look and behave somewhat like a number of real cell phones. It can also be used in conjunction with a third-party emulator that allows ...