J.E.N.I.
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:
Create a simple MIDlet
Create a Project in Netbeans
Create a MIDlet in Netbeans Mobility Pack
Run a MIDlet on the emulator
2.2 Getting Started
IDE (Integrated Development Environment) is a programming environment (programming
environment) that has a GUI builder, text or code editor, compiler and / or interpreter and
debugger. In this case, NetBeans Mobility Pack also has device emulator. This facility can
make us see our program on the device indeed.
2.3 "Hello, world!" MIDlet
We have learned in the previous section about the life cycle of a MIDlet (MIDlet's life
cycle). MIDlet began life when the MIDlet created by the Application Management System
(AMS) on the device.
Pengembangan Perangkat Mobile
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.
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
* @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);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
if (c == exitCommand){
destroyApp(true);
notifyDestroyed(); // Exit
}
}
}
Next we will learn our first MIDlet, focused on line importance of the code above:
public class HelloMidlet extends MIDlet implements CommandListener {
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.
public class HelloMidlet extends MIDlet implements CommandListener {
Code "implements CommandListener" is to command / key presses, so program we are
able to handle the "command" events. If we do implement CommandListener, we must
create a method commandAction ().
public void commandAction(Command c, Displayable d){
if (c == exitCommand){
destroyApp(true);
notifyDestroyed(); // Exit
}
}
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.
public void startApp() {
if (display == null){
display = Display.getDisplay(this);
}
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-1: HelloMidlet, , HelloMidlet
MIDlet-Jar-Size: 1415
MIDlet-Jar-URL: ProjectHello.jar
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.
2.4 Compilation and Packaging MIDlets
Before we use integrated tools to compile and perform packaging the MIDlet application
(MIDlet suite), we will try to use the command line.
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.
MIDlet application consists of:
File JAR
File Java Application Descriptor (JAD)
JAR files have:
File class
Manifest file describing the contents of the archive
The manifest file that describes the contents of the archive
Source: image / icon, video, data, etc. Used by applications
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
Furthermore, we are to compile Java source files:
javac -bootclasspath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar
*.java
Java Compiler program, "javac", must be on your path. If you see an error like "can not
find the file" or "not an executable", you can consult with installation guide for the
distribution of the Java development kit you about how enter the executable PATH of
location tools in Java.
Furthermore, we conduct pre-verify from the class file:
preverify
-classpath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar;.
-d . HelloMidlet
Preverify've been in the wireless toolkit from java.sun.com. Enter this command on a row.
The final step is to create the JAR fil:
jar cvfm HelloMidlet.jar manifest.txt HelloMidlet.class
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.
2.5 Using the Sun Wireless Toolkit
Now we are using Sun Wireless Toolkit for clicking compile and bundle application MIDlet /
MIDlet suite (containing one MIDlet)
Open ktoolbar (Wireless Toolkit distribution)):
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.
Copy HelloMidlet.java to a directory "src": In this window is in the directory: C: \ WTK23 \
apps \ HelloMidlet \ src (where C: \ WTK23 is where you are installing the wireless
toolkit). Click "Build" and "Run"
Pengembangan Perangkat Mobile
1
0
2.6 Using NetBeans Mobility Pack
As described earlier in this chapter about the necessary things, NetBeans Mobility Pack 4.1
and must have been installed on your computer.
Step 1: Create a new project
Step 2: Choose the category "Mobile"
Step 3: Choose the "Mobile Application"
Step 4: Name the project and specify its location
(Uncheck the "Create Hello MIDlet", we will make our own MIDlet later)
Step 5: Select Platform (optional)
Figure 1: newly created Mobile Project (NetBeans Mobility Pack)
Step 6: Create a new MIDlet
Step 7: Choose MIDP "Category" and MIDlet "File Type"
Step 8: Name the MIDlet
Step 9
Figure 2: Creating a MIDlet. MIDlet automatically create the required method
Pengembangan Perangkat Mobile
2
0
Step 10: Replace the code that is created automatically by our program code.
Step 11: Compile and Run (Run) MIDlet in Emulator
Step 12: Running MIDlet us in Emulator
Figure 3: Hello World MIDlet
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.
2.7.2Multiple MIDlets in the MIDlet suite using Wireless Toolkit
Use the Sun Wireless Toolkit to add a new MIDlet in the MIDlet application you.