J2ME Midlet Development
My previous J2ME article presented the basics of the J2ME platform. Specifically, it took a quick look at the CLDC and MID profile designed for mobile devices. Now it's time to take a quick look at the development tools built specifically for J2ME development. Leading the charge are the Sun J2ME Wireless Toolkit, the Motorola J2ME SDK, and the RIM Blackberry Java Development Environment. This article takes a look at these tools, finishing up with a ridiculously simple "Hello World" application, just to make sure your environment is set up correctly. The next article in this series will put J2ME through its paces through the construction of an address book app (requires GUI and data storage functionality).
Before We Begin…
Before getting started, there are a few J2ME programming basics that must be explained. To begin with, all applications using the MID profile are referred to as midlets. This might seem like just a cute name until you consider that all midlets extend the javax.microedition.midlet.MIDlet class (just as Java applets extend the Applet class). In addition to receiving input from a keyboard or pointing device, the MIDLet class also provides interfaces for invoking, pausing, and terminating the midlet, via the startApp(), pauseApp(), and destroyApp() methods, respectively. The startApp() method is similar in concept to a Java applet's start() method—it's called when the midlet starts but is also called each time a midlet is to resume after being paused.
One other class of immediate interest is the javax.microedition.lcdui.Command class. This class defines several semantic types that are commonly used on mobile devices: BACK, CANCEL, EXIT, HELP, ITEM, MENU, OK, SCREEN, and STOP. Commands are added to the user interface via the addCommand() method in the javax.microedition.lcdui.Displayable class (a parent class of all the J2ME UI components). The addCommand() method includes a priority parameter that allows an application to give the runtime environment "hints" on what to display in what order. In most environments, if two commands of the same type are added with the same priority level, the environment will display a "menu" option and allow the user to select from multiple command choices. The "Hello World" app at the end of this article includes support for the EXIT command.
The process of compiling, running, and deploying midlets differs a bit from the J2SE development process, so I'll also discuss that topic briefly here. The compilation process is the same—the end result is a Java class file. An extra step must then be completed known as preverification, in order to preprocess the class file for use by the K Virtual Machine (KVM). midlets must be packaged in a JAR file before they can be uploaded and run in a J2ME environment. This process involves the creation of a manifest file and a descriptor file containing essential information about the package. A predefined set of attributes must appear in every descriptor file. Let's not fret too much over the physical makeup of these files; tools such as the Sun J2ME Wireless Toolkit create these files for you and allow you to edit them inside a GUI. For more information on this topic, visit http://developer.java.sun.com/developer/technicalArticles/wireless/midpgetstart/index.html.