About MIDlet:
In J2ME a MIDlet is a Java application framework for the Mobile Information
Device Profile (MIDP). MIDlet is a MID Profile application which is
managed and controlled by the Application Management Software (AMS) built in the
device.
The application management software (AMS) is the software on a device that
manages the downloading and life-cycle of MIDlets. The AMS provides the runtime
environment for a MIDlet. It enforces security, permissions, and execution
states, and provides system classes and scheduling.
Just as J2SE applications have an entry point (any class with a main() method).
MIDlets javax.microedition.midlet.MIDlet class that offers the abstract
startApp() method which serves as the entry point to your MIDlet.
One should extend the abstract javax.microedition.midlet.MIDlet class or
in other words the MIDlet needs to be a subclass (derived) of (from)
javax.microedition.midlet.MIDlet class.
The javax.microedition.midlet.MIDlet class acts as an interface between
the MIDlet and the application management software.
Life-Cycle of MIDlet
Here we Understanding the MIDlet life-cycle is fundamental to creating any
MIDlet. The life-cycle defines the execution states of a MIDlet:
There are three possible states in a MIDlet's life-cycle as given below:
• Paused
• Active
• Destroy
MIDlet has three life-cycle notification abstract methods related to
MIDlet lifecycle: as given
• startApp()
• pauseApp()
• destroyApp(boolean unconditional
These methods need to be overridden by the MIDlet.
paused:
The MIDlet instance has been constructed and is inactive. pauseApp()
method causes the MIDlet to enter into paused State. This is where MIDlet lose
focus.
active: The MIDlet is active. startApp() method causes the MIDlet
to enter into active state. This is where the MIDlet gains focus.
destroyed: The MIDlet has been terminated. destroyApp(boolean unconditional)
method causes the MIDlet to enter in destroyed state.
The following definitions are used in the MIDlet lifecycle:
Application management software - a part of the device's software operating
environment that manages MIDlets. It directs the MIDlet through state changes.
MIDlet - a MIDP application on the device. The MIDlet can signal the
application management software about whether is it wants to run or has
completed.
MIDlet States - the states a MIDlet can have are defined by the
transitions allowable through the MIDlet.
MIDlet Transistions:
When a MIDlet is executed, initially it is in paused state. When the
execution environment changes focus to the executed MIDlet, midlets startApp()
is called causing MIDlet to switch into Active state and gains focus.
MIDlet now start receiving key events and rendering of the MIDlet
on physical display is shown. MIDlet becomes visible to the user; user
can now interact with the MIDlet.
If a Runtime exception occurs during startApp() the MIDlet will be
destroyed immediately. Its destroyApp(boolean unconditional) will be
called allowing the MIDlet to cleanup.
When the execution environment switch focus from the executing MIDlet, its
pauseApp() is called. MIDlet now enters into Paused state. Key events will not
be consumed by the MIDlet in Paused. MIDlet that is currently in Active state
consumes Key events. Also MIDlet may continue to render in background. However
rendering of the MIDlet will not be visible on physical display. In the Paused
state the MIDlet must release shared resources and become quiescent.
If a Runtime exception occurs during pauseApp the MIDlet will be destroyed
immediately. Its destroyApp(boolean unconditional) will be called
allowing the MIDlet to cleanup.
Developer should implement startApp() and pauseApp() properly as
these methods are called as an when MIDlet transits from Active state to Paused
state.
Also when MIDlet regains Active state, MIDlet doesn’t have knowledge of its
suspension. Common example is of time based game MIDlet. When MIDlet enters into
Paused state, time calculation should be stopped and should start only when
MIDlet regains focus.
When developer exits MIDlet, ( destroyApp(boolean unconditional) should
be called by developer ), destroyApp(boolean unconditional) signals the
MIDlet to terminate and enter the Destroyed state. In the destroyed state the
MIDlet must release all resources and save any persistent state. This method may
be called from the Paused or Active states also.
MIDlets should perform any operations required before being terminated, such as
releasing resources or saving preferences or state.
The destroyApp(boolean unconditional) method provides an unconditional
parameter; if it is set to false, the MIDlet is allowed to refuse its
termination by throwing a MIDletStateChangeException. If the parameter is
set to true, the AMS terminates the MIDlet irrespective of what state the MIDlet
is in.
If a Runtime exception occurs during destroyApp then they are ignored and
the MIDlet is put into the Destroyed state.
Initiating MIDlet StateChange
MIDlet can initiate some state changes itself and notifies the application
management software of those state changes by invoking one of these methods:
notifyDestroyed(): This method is used by a MIDlet to notify the
application manager that it has entered into the destroyed state. The
application management software will not call the MIDlet's destroyApp(boolean
unconditional) method
notifyPaused(): This method notifies the application management software
that the MIDlet does not want to be active and has entered the paused state.
Invoking this method will have no effect if the MIDlet is destroyed, or if it
has not yet been started. It may be invoked by the MIDlet when it is in the
Active state.
resumeRequest(): This method provides a MIDlet with a mechanism to
indicate that it is interested in entering the active state. Calls to this
method can be used by the application management software to determine which
applications to move to the active state. When the application management
software decides to activate a MIDlet, it will call that MIDlet's startApp()
method. Figure 2 shows MIDlet transition.
API Description
Each MIDlet must extend the abstract MIDlet class found in the
javax.microedition.midlet.* package, much like creating an applet by
extending the java.applet.Applet class.
At the minimum, your MIDlet must override three methods of this abstract class,
startApp(), pauseApp(), and destroyApp(boolean unconditional).
Following are the some other defined methods in MIDlet class is given
below:
MIDlet class methods:
Methods | Description |
int checkPermission(String permission) | Get the status of the specified permission. |
protected abstract void destroyApp(boolean unconditional) | Signals the MIDlet to terminate and enter the Destroyed state. |
String getAppProperty(String key) | Provides a MIDlet with a mechanism to retrieve named properties from the application management software. |
void notifyDestroyed() | Used by MIDlet to notify the application management software that it has entered into the Destroyed state. |
void notifyPaused() | Notifies the application management software that the MIDlet does not want to be active and has entered the Paused state. |
protected abstract void pauseApp() | Signals the MIDlet to enter the Paused state. |
boolean platformRequest(String URL) | Requests that the device handle (for example, display or install) the indicated URL. |
void resumeRequest() | Provides a MIDlet with a mechanism to indicate that it is interested in entering the Active state. |
protected abstract void startApp() | Signals the MIDlet that it has entered the Active state. |
Sample MIDlet Code:
Class: HelloMIDlet
Following Sample code shows MIDlet Life Cycle.
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloMIDlet extends MIDlet {
public HelloMIDlet()
{
/*Intialise*/
System.out.println(“Constructor Invoked”);
}
/*Enters MIDlet into active state*/
protected void startApp() {
System.out.println(“startApp Invoked”);
/*Do all your getDisplay() , setCurrent(), and other one time initialization*/
}
/*Enters MIDlet into pause state*/
protected void pauseApp() {
System.out.println(“pauseApp Invoked”);
}
/*Enters MIDlet into destroy state*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
{
System.out.println(“destroyApp Invoked”);
notifyDestroyed();
}
}