Beruflich Dokumente
Kultur Dokumente
1 Objectives
In this section, we will delve into writing, building, using the emulator, and packaging
J2ME applications. The Integrated Programming Environment that we will use is
Netbeans 4.1 (www.netbeans.org) with Mobility Pack.
2 Introduction
We have studied in the previous section the MIDlet's life cycle. A MIDlet's life starts when
it is created by the Application Management System (AMS) of the device. Initially, it is in
the "paused" state.
To be able to create a MIDlet, we must create a subclass of the MIDlet class from
javax.microedition.midlet package. We must also override or implement the methods:
startApp(), destroyApp() and pauseApp(). These are the methods expected by the AMS
for running and controlling our MIDlet.
new
destroyApp()
startApp() Paused
Destroyed
Active pauseApp()
destroyApp()
Unlike a typical Java program where the main() method is only entered once in the
program's life, the startApp() method may be called more than once during the MIDlet's
life cycle. So we must not put one time initialization codes in the startApp() method.
Instead, we can create a MIDlet constructor and do our initializations there.
/*
* HelloMidlet.java
*
* Created on July 8, 2000, 9:00 AM
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author JEDI Apprentice
* @version
*/
public class HelloMidlet extends MIDlet implements CommandListener {
Display display;
Command exitCommand = new Command("Exit", Command.EXIT, 1);
Alert helloAlert;
public HelloMidlet(){
helloAlert = new Alert(
"Hello MIDlet", "Hello, world!",
null, AlertType.INFO
);
helloAlert.setTimeout(Alert.FOREVER);
helloAlert.addCommand(exitCommand);
helloAlert.setCommandListener(this);
}
display.setCurrent(helloAlert);
}
Next, we will dissect our first MIDlet, focusing on every significant line of code:
As we have said before, we should create a subclass of MIDlet to create our MIDP
program. In this line, we created a subclass of MIDlet by extending it and naming it
HelloMIdlet.
Display display;
Command exitCommand = new Command("Exit", Command.EXIT, 1);
Alert helloAlert;
These are the variable properties of our MIDlet. We will need the Display object (there is
only one display associated per MIDlet) in order to do some drawing on the screen. The
exitCommand is a command we must put in our screen so that we can quit the program.
If we do not put any exit command, there will be no way to exit our MIDlet gracefully.
public HelloMidlet(){
helloAlert = new Alert(
"Hello MIDlet", "Hello, world!",
null, AlertType.INFO
);
helloAlert.setTimeout(Alert.FOREVER);
helloAlert.addCommand(exitCommand);
helloAlert.setCommandListener(this);
}
The constructor initializes our Alert object. We will be discussing more of the Alert class
in the next sections. The addCommand() method on the Alert object puts an "Exit"
command on the screen. The setCommandListener() method tells the system to pass all
command events to our MIDlet.
}
}
Our commandAction() only handles request for the "Exit" command. Issuing or pressing
the "Exit" command will terminate our program using notifyDestroyed().
display.setCurrent(helloAlert);
}
This is the entry point of our program once it is ready to be displayed by the AMS.
Remember that the startApp() may be entered more than once in the MIDlet's life cycle.
If a MIDlet is paused, say by an incoming phone call, it would enter the paused state
(pauseApp). If after the call the AMS wants to bring back our program, it would call the
startApp() method again. The display.setCurrent() method tells the system that we want
our Alert object to be displayed on the screen. We can get the display object by calling
the static Display.getDisplay() method.
Netbeans automatically creates a Java Application Descriptor (JAD) for your program.
Netbeans put the JAD file in the "dist" folder under the project's folder. Here is a sample
JAD file created by Netbeans:
As a pre-requisite for this section, Netbeans 4.1 and Mobility Pack should be installed on
your computer.
Step 10: Replace the automatically created code with our program's code.
5 Exercises
Add a new MIDlet to our project "ProjectHello". Take note that Netbeans automatically
adds the new MIDlet in the the applications JAD file when you use the "New File..."
Wizard.