MIDlets are an important part of J2ME technology. A MIDlet is simply a Java class with special features. To execute a MIDlet on a device, the target device should implement Java ME, CLDC and MIDP to run. MIDlets posses "compile once, run anywhere" feature which makes them very portable. MIDlet are distributes using jar and jad files file.

A MIDlet class whould have the following:

- The main class should extend javax.microedition.midlet.MIDlet.
- The MIDlet class should be packed into a JAR file for deployment.
- The .jar file needs to be pre-verified by using a preverifier.
- Sometimes it is important to sign the JAR file by the mobile phone's carrier.

MIDlet Lifecycle

A MIDlet has 4 different states: loaded, active, paused, and destroyed.

Loaded: A MIDlet is said to be in loaded state, when it is loaded into the device and the constructor is called. This is done a bit before when the AMS (Application Management Software) starts the application by calling the startApp() method.

Active: startApp() method is called to bring the MIDlet in active state. A MIDlet remains in the active state unless the AMS calls pauseApp() or destroyApp().

Paused: AMS calls pauseApp() method to pauses the MIDlet.

Destroyed: AMS calls desroyApp() method to terminate the MIDlet. Doing so will release all the resources occupied by the MIDlet.

Note: State is only changed when a method returns. So the change callback method should terminate quickly.

Review the method signatures in the given MIDlet. I will present more meaningful examples later.

Code:

import javax.microedition.midlet.*;

public class HelloMIDlet
extends MIDlet
{
public HelloMIDlet() {
…
}

public void startApp() {
…
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

}

MIDlet suite

MIDlet suite is very much like a standard Java project in Eclipse. Please note that it’s not a requirement to place MIDlets inside a Java Archive (JAR) file, but JAR files provide the most common means of distributing MIDP applications. The MIDlet Suite comproses of all files and resources that may be required as part of a MIDlet. The MIDlet Suite consists of

The toolkit will help us in building the source code and to run it on the emulator. It does not provide development environment. We will be writing MIDlets in text editors.

IDEs for MIDlet development

You need an environment for MIDlet development. You may use JBuilder, Eclipse or NetBEans with appropriate plugins installed. You will get an emulator for testing your MIDlets which is very handy. A standard mobile emulator is shown below:

which option to choose, depends on the cell phone you have. We will need a USB cable for the first option, which is provided by your phone manufacturer and also there will be a software from the manufacturer. Do read the manual before opting this.

You may also use InfraRed and Bluetooth options as well. If your phone and your PC both have InfraRed, then you can transfer files via InfraRed. This is very useful option if you are suing Windows XP.

MIDlet deployment - JAD files

JAD (Java Application Decription files) contains the JAR file name and size attributes. From mobile phone, you will make an HTTP request for the JAR file. In response, you will get the JAD file on to your mobile device. Mobile device have AMS (Application Management System) that will read the required attributes and will make another HTTP request to the server for the required JAR file. In response, you will get the JAR file on to your mobile device which actually is the MIDlet.

We use MIDP high level APIs for creating GUI components. I will present some very important high level APIs.

Display

MIDlets can be used for processing only which means no GUI required. But mostly, you would like the users to interact with your MIDlet. In such case, you need to get the display working. This is done by obtaining an instance of the Display class. For that, call Display.getDisplay (MIDlet midlet), where the MIDlet itself is given as parameter.

The classes related to user interfaces are located in javax.microedition.lcdui. So, import it for use. There is a setCurrent() method of Display class which is used to set the current display content of the MIDlet. The setCurrent() method notifies the AMS that the MIDlet would like to have the given Displayable object displayed. There is a slight difference between Display and Displayable. The Display class represents the display hardware, whereas Displayable is something that can be shown on the display.

Let talks about Screen class. It is an abstract class but has some functionalities. Each Screen can have two characteristics which are title and ticker. The title is a String that appears above the screen contents and the ticker is a graphical component that appears above the title. This ticker can be used to scroll information across to the user. Please note that both of these characteristics are optional. The following figure shows titles and tickers on a screen.

We use TextBox object to allow user to enter and edit text. It is similar to textboxes we see daily on the web. We may use TextBox in MIDlets to get name, a phone number, an email address, or a password etc from the user. One needs to provide 4 parameters while creating a TextBox. The constructor reads like:

Lets talk about each parameter. The title is simply the screen title. The text and maxSize are for the default text that will appear in the TextBox. The last parameter is used to limit the user's input. The available constraints are as follows:

Code:

TextField.ANY // The user is allowed to enter any character.
TextField.EMAILADDR // Input must be an email address.
TextField.NUMBER // Input must be an integer value.
TextField.PASSWD //The text entered will be
//masked (replaced by asterisks), so the characters typed are not visible.
TextField.PHONENUMBER // Input must be a phone number.
TextField.URL // Input must be a URL.

The MIDP low-level APIs provides little abstraction. They are for applications that need precise placement and control of graphic elements and access to low-level input events. This API gives the application full control over what is being drawn on the display. Examples of classes that implement the low-level API are Canvas and Graphics.

Conclusions

MIDlet programming is very easy and fun. If you are using an IDE for development, then you will really enjoy the development, since the emulator is built in.

MIDLet user interface components are different from AWT and Swing components. They are light weight and simple. Programming these components is very easy as compared with the AWT and Swing libraries.