CH 02
CH 02
Chapter 2
Starting Mobile Programming
2.1 Objectives
In this section, we will be delving 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) and NetBeans Mobility Pack.
So that we can create a MIDlet, we must create a subclass of the MIDlet class of
javax.microedition.midlet package. We also need to override or implement the method:
startApp (), destroyApp () and pauseApp (). Those methods are is the method required
by AMS to run and control the MIDlet.
new
destroyApp()
startApp()
Paused
Destroyed
pauseApp()
Active
destroyApp()
Unlike the Java program in general where the method main () is only used once in the
course of the program, method startApp () may be called more than once in the MIDlet
life cycle. So you are required to not make one initialization code in the method startApp
(). Instead, you can create a MIDlet consturctor and initializing there.
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);
}
public void startApp() {
if (display == null){
display = Display.getDisplay(this);
}
display.setCurrent(helloAlert);
}
}
}
Next we will learn our first MIDlet, focused on line importance of the code above:
As we have said before, we have to create a subclass of MIDlet to make MIDP program.
In this line, we've made a subclass of MIDlet by giving the parent class derivative and
named HelloMIDlet.
Display display;
Command exitCommand = new Command("Exit", Command.EXIT, 1);
Alert helloAlert;
The above line is the variable properties of the MIDlet. We need an object Display (only
one diplay per MIDlet) to perform the function of drawing on the screen. exitCommand is
a command that will we put on the screen so that we can get out of the program. If we
do not have the orders out, then we have no way to exit the MIDlet properly.
public HelloMidlet(){
helloAlert = new Alert(
"Hello MIDlet", "Hello, world!",
null, AlertType.INFO
);
helloAlert.setTimeout(Alert.FOREVER);
helloAlert.addCommand(exitCommand);
helloAlert.setCommandListener(this);
}
Constructor perform initialization of object Alert. We will learn more from the class Alert in
the next chapter. Method addCommand () on the object Alert giving the command "Exit"
on the screen. Method setCommandListener () provides information to the system to
provide all the command events to the MIDlet.
commandAction() only to handle the request for the command "Exit". method above will
stop the program using notifyDestroyed () if the command "Exit" executed or suppressed.
display.setCurrent(helloAlert);
}
The code above is the first part of our program when we are ready to program displayed
by AMS. Keep in mind that the method startApp () may / can entered more than once as
the MIDlet lifecycle. If the MIDlet stop / terminated, such as when a call comes in, the
program will go to state stops (pausedApp). If the call has been completed AMS will
return to the program and startApp invoke () again. Method display.setCurrent () provides
information to the system that we want the Alert object to appear on the screen. We can
got tampilah object by calling the static method Display.getDisplay ().
NetBeans Mobility Pack automatically creates a Java Application Descriptor (JAD) for
your program. NetBeans Mobility Pack put the JAD file in the folder "dist" from project
folder. Here is an example of the JAD file created by the NetBeans Mobility Pack:
MIDlet-Name: ProjectHello
MIDlet-Vendor: Vendor
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0
Now we are ready to compile, do packaging (package) on the application Our first MIDlet.
MIDlet applications are typically bundled into a file that is a JAR file. This file is
compressed files, such as ZIP files. On implementation, you can open the JAR file using a
ZIP file decompressor program.
File JAR
File class
The manifest file, MANIFEST.MF is like a JAD file. This file is used by appication manager of
the device. Some fields that are required by the manifest file is:
MIDlet-Name
MIDlet-Version
MIDlet-Vendor
MIDlet-<n> (where n is a number from 1, for each MIDlet in the JAR file)
MicroEdition-Profile
MicroEdition-Configuration
Preverify've been in the wireless toolkit from java.sun.com. Enter this command on a row.
Program jar was already in the Java Development Kit, and its location must be entered
the executable path. This command creates a JAR file with the file name HelloMidlet.jar.
Manifest.txt file name was changed to the MANIFEST.MF in the JAR file.
Create a project:
In the Settings window, you can change many options of several options configuration for
your project. You can choose the configuration that will work, package / API required,
configuration Push Registry and others. For our purposes this time, we will use the default
configuration of the project. Click "OK" to finish making project.
1
0
Step 9
2
0
Step 10: Replace the code that is created automatically by our program code.
2.7 Exercises
2.7.1 Multiple MIDlets within a MIDlet suite
Add a new MIDlet in the project "ProjectHello". You need to note that NetBeans Mobility
Pack automatically add new MIDlet application file JAD when you use the "New File ..."
Wizard.