Starting Mobile Programming: 2.1 Objectives
Starting Mobile Programming: 2.1 Objectives
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.
At the end of the lesson, the student should be able to:
J.E.N.I.
new
destroyApp()
startApp()
Paused
Destroyed
Active
pauseApp()
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 constructor and initializing there.
Here is our first MIDP program code
/*
* HelloMidlet.java
*
* Created on July 8, 2000, 9:00 AM
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author
JEDI Apprentice
J.E.N.I.
* @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);
}
J.E.N.I.
}
}
Next we will learn our first MIDlet, focused on important line 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.
J.E.N.I.
display.setCurrent(helloAlert);
}
The code above is the first part of our program when we are ready to display the program
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 it will
return to the program and invoke startApp() again. Method display.setCurrent() provides
information to the system that we want the Alert object to appear on the screen. We can
get the 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:
J.E.N.I.
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 our first MIDlet application.
File JAR
File class
The manifest file, MANIFEST.MF is like a JAD file. This file is used by application 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)
J.E.N.I.
MicroEdition-Profile
MicroEdition-Configuration
preverify
-classpath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar;.
-d . HelloMidlet
Pre-verify have been in the wireless toolkit from java.sun.com. Enter this command on a
row.
The final step is to create the JAR file:
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.
J.E.N.I.
Create a project:
In the Settings window, you can change many options of several configuration options 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.
J.E.N.I.
J.E.N.I.
10
J.E.N.I.
11
J.E.N.I.
12
J.E.N.I.
13
J.E.N.I.
14
J.E.N.I.
15
J.E.N.I.
16
J.E.N.I.
17
J.E.N.I.
18
J.E.N.I.
19
J.E.N.I.
Step 9
20
J.E.N.I.
Step 10: Replace the code that is created automatically by our program code.
21
J.E.N.I.
22
J.E.N.I.
23
J.E.N.I.
24
J.E.N.I.
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.
25