Introduction
Mobile technology is a day-to-day technology that almost everyone has. Some even use this technology more than PC and they are always in need for new features and functionality. These features and functionality are provided using Mobile Applications, so I decided to write a series of articles about building mobile applications.
Background
J2ME applications are simply Java applications with a limited functionality that are designed to run on mobile devices. The standard Java runtime environment for these devices is provided using Mobile Information Device Profile (MIDP) combined with the Connected Limited Device Configuration (CLDC). MIDP and CLDC simply provide the core application functionality required by mobile applications. To write J2ME applications, you need the Java Platform Micro Edition SDK and an IDE, which can be Netbeans or Eclipse.
Creating MIDlet
All J2ME applications must have a main class which should be derived from a special class called MIDlet
. It is like the entry point for the application and manages the life cycle of the application. The MIDlet
can be in one of three states: active, paused and destroyed. MIDlet
is put into a paused state when the application manager calls pauseApp()
method, and when it calls startApp()
method the MIDlet
is put into the active state. The destroyed state is entered when the MIDlet
destroyApp()
method is called or when the MIDlet
itself calls notifyDestroyed()
method.
The MIDlet
class is located in the javax.microedition.midlet
package, so the first thing to do is to import this package in addition to the javax.microedition.lcdui
which contains the J2ME UI components.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
The next step is to create a new class derived from MIDlet
class, and to implement the three abstract
methods: startApp()
, pauseApp()
and destroyApp()
.
public class HelloMIDlet extends MIDlet {
public HelloMIDlet() {
}
protected void startApp() {
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
The Display and Displayable
The J2ME application can run in the background or interact with the user. Interactive applications need access to mobile display by obtaining an instance of Display
class. The Display
object is used to display the required user interface which should be derived from the Displayable
class, in this article an instance of the Form
class will be used. The Form
is a derived class of the Displayable
class, which has a title and you can add contents to it using other UI components.
So the next step is to define two data members of the Display
and Form
classes.
public class HelloMIDlet extends MIDlet {
private Display display;
private Form helloFrm;
...
}
In the constructor of MIDlet
class, creates an instance of Form
class by calling the constructor and passing the form title to it.
public HelloMIDlet() {
helloFrm = new Form("Hello World");
}
Display The Form
The next step is to obtain an instance of the Display
class and display the form. To get an instance of the Display
class, you call the Display
class static
method getDisplay()
and passing a reference to the MIDlet
object to it. Next, use the Display
object setCurrent()
method to display the Form
.
protected void startApp() {
display = Display.getDisplay(this);
display.setCurrent(helloFrm);
}
Conclusion
This article describes how to create a J2ME Hello World mobile application. The first step which is required for all J2ME applications is to create the MIDlet
class, which is the main class in your application. Then you can use an instance of the Form
class as the main UI in the application, which should be displayed using an object of the Display
class.
Following is the complete application code:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloMIDlet extends MIDlet {
private Display display;
private Form helloFrm;
public HelloMIDlet() {
helloFrm = new Form("Hello World");
}
protected void startApp() {
display = Display.getDisplay(this);
display.setCurrent(helloFrm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
}
History
- 17th March, 2011: Initial version