Understanding J2ME Midlet

Understanding J2ME Midlet




MIDlet has some resemblance with Applet. Like applets, it doesn’t have a main function and is controlled by an outer environment (web browser for applets, mobile devices for J2ME). However the analogy is only to help you appreciate MIDlets as a different entity from stand-alone Java applications, the implementations are widely different.


Let’s have a look at the code we used and analyze it.



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet {
  private Form mMainForm;

  public HelloMIDlet() {
    mMainForm = new Form("HelloMIDlet");
    mMainForm.append(new StringItem(null, "Hurray! My MIDlet works!"));
  }
  
  public void startApp() {
    Display.getDisplay(this).setCurrent(mMainForm);
  }
  
  public void pauseApp() {}

  
  public void destroyApp(boolean unconditional) {}
}

First notice the import commands. It includes packages from the micro edition of java i.e. the J2ME environment. There are some standard Java packages available too but not all. As you remember, J2ME is the little brother of standard Java.

Any J2ME application contains a class extending javax.microedition.midlet.MIDlet class like HelloMIDlet in this case. Note that there is no ‘main’ function. This is because the MIDlets run in a particular environment where its functioning is controlled by the platform’s Application Management Software (AMS). The MIDlet application can be in 3 states –

j2me midlet lifecycle

When you start an application the AMS first creates the MIDlet using the constructor. It is initially in paused state. AMS calls the startApp() function to make the MIDlet active. This is where all the action begins. The application is allotted required resources and it performs the actions it is supposed to perform.

However in a resource-limited device, it might be necessary to use the same resources for a more important action like handling an incoming call. In such a case, AMS calls the pauseApp() function. The MIDlet goes into paused state and relieves resources required for the more urgent business. The application itself can voluntarily go into pause state calling notifyPaused().Otherwise, in case of an error (e.g. MIDletStateChangeException thrown by startApp()), the MIDlet takes the safe road of staying in the paused state.

If at any point of time AMS decides that the application no longer needs running (e.g. when you close an application pressing a button), it calls the destroyApp(boolean unconditional) method. Here the application might do some last minute action before getting destroyed (e.g. storing a value, closing an open connection etc.). The application also might destroy itself calling notifyDestroyed(). In that case destroyApp() is not called and the application should do the last minute actions before calling notifyDestroyed().

However if AMS calls destroyApp() while midlet is doing a critical operation, the application might not like to get destroyed. That is when the unconditional argument comes into the picture. If it is true, the application must get destroyed no options left, but if it is false the application has an option to refuse getting destroyed throwing a MIDletStateChangeException. Please keep in mind that in such a case AMS might postpone the destruction and call destroyApp(boolean unconditional) at a later stage or it might still force a destruction. What actually happens is device dependent.

Don’t worry about classes like Form or StringItem right now. These are part of the javax.microedition.lcdui package used to design the user interface in LCD screen of devices. We will go through them while learning about J2ME UI.

So by now you know what J2ME is, the basic structure and functioning of J2ME applications and also how to use the Wireless Toolkit (WTK) to develop your applications.

I suggest that you first learn about basic J2ME UI, as they will be used in each program, and then start with the location API JSR-179. To complete the whole of the LBS tutorial, you might need to know other features of J2ME but you may skip those for a moment. I will inform you at the starting of a lesson, if you need to know some of those features to continue.