MIDlet application is an applet that runs in small devices. Like an applet the midlet has a life cycle. The core package which addresses the midlet life cycle is javax.microedition.midlet. Each state of life cycle is notified to Application Management Software(AMS) and the AMS acts appropriately. AMS is the device software that handles the midlet lifecycle.
AMS interacts with the MIDlet application in loading and unloading the MIDlet using the MIDlet's life cycle methods and thus affecting the following midlet states.
MIDlet class is an Abstract class and your application's root class must extend this class and override its abstract methods. Let us jump into to the HelloWorld code to understand this better.
Midlet Instantiation and initial paused phase
This phase uses JAD file, manifest file and HelloWorld.java file to start an application. The "MIDlet-1" parameter is defined in HelloWorld.JAD and MANIFEST.MF as
MIDlet-1: HelloWorld, HelloWorld.png, com.jme.hello.HelloWorld |
AMS calls The MIDlet-1 property
"com.jme.hello.HelloWorld" specified in the JAD file and the HelloWorld()
constructer is loaded. Some devices pick up this property from the manifest
file. Here Midlet enters paused state.
Code Segment of HelloWorld.java
public class HelloWorld extends MIDlet
implements CommandListener {
private Form helloForm;
private Command exitCommand;
/**
* Midlet constructor is called
* Essential Objects are instantiated.
* Midlet enters paused state
* */
public HelloWorld() {
exitCommand = new Command("Exit", Command.EXIT, 1);
helloForm = new Form("Hello World");
helloForm.append(new String("Hello World"));
helloForm.addCommand(exitCommand);
helloForm.setCommandListener(this);
}
|
This HelloWorld class implements
CommandListener which is an event handling class of the MIDP. HelloWorld.java
also uses lcdui package for Graphical User Interface. We will discuss these
classes in detail in later sections.
Active State
/**
* startApp() method is called to change
* paused state to active state.
*/
protected void startApp() {
Display.getDisplay(this).setCurrent(helloForm);
}
|
Midlet's startApp() method
is called to change the paused state to active state. Here the midlet is displayed
on Electronic Data Interface(EDI) screen of the mobile phone. The midlet is
ready to react to any user interaction as the midlet implements Command Listener
Interface.
Paused State
/**
* The MIDlet frees as much resources as it can.
*/
protected void pauseApp() {}
|
When MIDlet's pauseApp() is called. The MIDlet frees as much resources as it can. startApp() can be called after paused state and the application becomes active again.
Destroyed State
/**
* Midlet cleans up resources before the application exits.
*
*/
protected void destroyApp(boolean bool) {}
|
MIDlet.destroyApp() is used to do cleaning up before the application exits.
Notify AMS about application Exit
public void commandAction(Command cmd, Displayable disp) {
if (cmd == exitCommand) {
destroyApp(false);
// Midlet notifies the AMS that it has done its work.
notifyDestroyed();
}
}
|
notifyDestroyed() is called
to notify the AMS that the application has exited. AMS gets the handle.
Analogy between MIDlet life cycle and the HelloWorld Program
Complete code of HelloWorld.java
package com.j2me.salsa.hello;
//Midlet's User interface API
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
//Midlet class's package
import javax.microedition.midlet.MIDlet;
public class HelloWorld extends MIDlet
implements CommandListener {
private Form helloForm;
private Command exitCommand;
/**
* Midlet constructor is called
* The objects are instantiated.
* Midlet enters paused state
* */
public HelloWorld() {
exitCommand = new Command("Exit", Command.EXIT, 1);
helloForm = new Form("Hello World");
helloForm.append(new String("Hello World"));
helloForm.addCommand(exitCommand);
helloForm.setCommandListener(this);
}
/**
* startApp() method is called to
* change the paused state to active state.
*/
protected void startApp() {
Display.getDisplay(this).setCurrent(helloForm);
}
/**
* The MIDlet frees as much resources as it can.
*/
protected void pauseApp() {}
/**
* Midlet cleans up resources before exit.
*
*/
protected void destroyApp(boolean bool) {}
/**
* Midlet event handling
*/
public void commandAction(Command cmd, Displayable disp){
if (cmd == exitCommand) {
destroyApp(false);
// Midlet notifies the AMS that it has done its work.
notifyDestroyed();
}
}
}
|
This brings us to the end
of the skeleton of midlet programming. Midlet class is similar to lots of other
java related life cycles like Applets and Servlets. This is the class where
the J2ME coding begins. In next section we will discuss the compiling/build
process and also go through other important MIDlet class methods.
| j2meSalsa goodies | |
| Execute Hello World Sample online | Select "Hello World" on the online emulator on your left frame. |
| Download demo code for deploying directly to WTK | Click here to download Zip File Containing WTK compatible file structure. |
site comments powered by Disqus
This page is a part of
a frames based web site. If you have landed on this page from a search engine
click here to view the complete page.