Say hello to J2ME: A programming tutorial
Java 2, Micro Edition (J2ME) gives Java developers the opportunity to leverage their skills onto a wide variety of mobile devices. Of course, mobile devices are diverse in their capabilities and horsepower, but J2ME abstracts away these differences by defining configurations and profiles which, when taken together, provide a complete platform and Application Programming Interface (API) for application development on a particular family of mobile devices. The Connected Limited Device Configuration (CLDC) and Mobile Information
Device Profile (MIDP) combine to support the majority of low-cost mobile devices
in use today, such as PDAs, wireless phones and two-way pagers. In this
article, you'll get your feet wet with J2ME by building a small sample
application.
The MIDlet class provides three abstract methods that are used by the device's application manager to communicate with the apps it runs. The startApp method is called immediately after the constructor and anytime an application is made active, not just when the application is initially launched. An application may transition between active and inactive states multiple times during a single run, so you should avoid placing single-use initialization code here, such as code to initialize the UI, since it will likely be executed a number of times. Use the constructor for this purpose instead. The destroyApp method is called by the manager to indicate that an application is about to be shut down. Unlike startApp, this method will be called only once during an application's lifetime, so it's safe to place cleanup code here. In fact, because the MIDP doesn't include finalized functionality for objects, you'll have to perform cleanup here. At the same time, though, a typical mobile device is much less stable than your typical standard platform and will be routinely powered off or reset by the user. So you can't really count on destroyApp being executed either. The remaining abstract method, pauseApp, is a little weird at first blush. It's meant to provide notification that the app is about to be paused because the user has switched to a different application or is using a function of the device that will prevent the app from continuing to run. Because most mobile devices lack the horsepower to be truly multitasking, this will probably happen to your app a lot. You should also be a good citizen and free any resources you can inside this method. When the application is resumed, the startApp method will be called by the application manager.
Communicating with the application manager
As I mentioned before, the javax.microedition.lcdui package contains the UI elements used with MIDP applications. Most of the UI elements you'll find here have analogous Swing counterparts, although the names are different, and the underlying event system works in essentially the same way for both APIs. You'll still be registering event listener objects with controls, as I do in HelloJ2ME's constructor. In this case, for simplicity, HelloJ2ME implements the commandListener interface itself and serves as the listener object for the sole Command object it contains. However, other techniques for creating event listener classes, anonymous inner classes, and dedicated classes will work just as well. Meet the family Screen components Item components Miscellaneous Displayable components Figure B illustrates the hierarchical arrangement of these components.
The denizens of the lcdui package The whole graphical shebang is managed by a Display object, of which each application has access to a single, private instance. This instance is retrievable via the static Display.getDisplay method, and it's customary to hold a single reference to this instance in a member variable, as HelloJ2ME does in its constructor. In addition to methods for setting the focus to a particular screen element (setCurrent) and retrieving the element with the focus (getCurrent), Display also exposes a handful of methods useful for obtaining information about the display capabilities of the device, notably whether color is displayed (isColor) and how many colors are supported (numColors).
Things to remember |
- Talkback
-



