You are on page 1of 24

J.E.D.I.

Getting Started with


Mobile Programming

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) with Mobility Pack.

After finishing this lesson, the student should be able to:

• Create a simple MIDlet


• Create a Project in Netbeans
• Create a MIDlet in Netbeans
• Run a MIDlet on the emulator

2 Introduction

An IDE (Integrated Development Environment) is a programming environment with a


GUI builder, a text or code editor, a compiler and/or interpreter and a debugger. In our
case, Netbeans also comes with a device emulator. This lets you see how your program
would look like in an actual device.

3 "Hello, world!" MIDlet

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.

Mobile Application Development 1


J.E.D.I.

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 you must not put one time initialization codes in the startApp() method.
Instead, you can create a MIDlet constructor and do your initializations there.

Here is the code of our first MIDP program:

/*
* 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 {

Mobile Application Development 2


J.E.D.I.

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 would dissect our first MIDlet, focusing on every significant line of code:

Mobile Application Development 3


J.E.D.I.

public class HelloMidlet extends MIDlet implements CommandListener {

As we have said before, we should create a subclass of MIDlet to create our MIDP
program. In this line, we did 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 would need the Display object (these
is only one display associated per MIDlet) in order to do some drawing on the screen.
The exitCommand is a command we would put in our screen so that we can quit the
program. If we would not put any exit command, there would 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 would 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.

public class HelloMidlet extends MIDlet implements CommandListener {

The "implements CommandListener" code is for command/key presses, so that our


program would be able to handle "command" events. If we implement a
CommandListener, we must create a commandAction() method.

public void commandAction(Command c, Displayable d){


if (c == exitCommand){

Mobile Application Development 4


J.E.D.I.

destroyApp(true);
notifyDestroyed(); // Exit
}
}

Our commandAction() only handles request for the "Exit" command. It would terminate
our program using notifyDestroyed() if the "Exit" command is issued or pressed.

public void startApp() {


if (display == null){
display = Display.getDisplay(this);
}

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 a sample
JAD file created by Netbeans:

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're ready to compile, package and run our first MIDlet.

Mobile Application Development 5


J.E.D.I.

4 Compilation and Packaging MIDlets

Before we use any of the integrated tools for compiling and automatically packaging our
MIDlet suites, we'll try to do this using the command line tools.

The MIDlet suite is usually packed into a single file called the JAR file. This is a
compressed file, much like a ZIP file. In fact, you can open a JAR file using a ZIP
decompressor program.

The MIDlet suite consists of:

– The JAR file


– The Java Application Descriptor (JAD) file

The JAR file contains:


– The class files
– Manifest file describing the contents of the archive
– resources: images/icons, video, data, etc. used by the suite

The Manifest file, manifest.mf is like the JAD file. It is used by the application manager of
the device. The required fields of the manifest file are:

– 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

First we compile the java source files:

javac -bootclasspath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar


*.java

Mobile Application Development 6


J.E.D.I.

The Java Compiler program, "javac", should be on your path. If you see an error like,
"cannot find file" or "not an executable", please consult the installation guide for your
Java development kit distribution on how to put in your executable PATH the location of
the java tools.

Next, we pre-verify the class file(s):

preverify
-classpath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar;.
-d . HelloMidlet

preverify is included in the wireless toolkit from java.sun.com. Enter this command in a
single line.

The final step is to create the JAR itself:

jar cvfm HelloMidlet.jar manifest.txt HelloMidlet.class

The jar program is included in the Java Development Kit, and its location should be
included in your executable path. This command will create the JAR file with filename
HelloMidlet.jar. The manifest.txt would be renamed to manifest.mf in the JAR file.

Mobile Application Development 7


J.E.D.I.

5 Using the Sun Wireless Toolkit

We'll now use the Sun Wireless Toolkit to compile and package our MIDlet suite
(containing a single MIDlet).

Open ktoolbar (from the Wireless Toolkit distribution):

C reate a new project:

Mobile Application Development 8


J.E.D.I.

On this Settings window, you can customize the a very comprehensive selection of
settings for your project. You can select what configuration to use, what packages/APIs
to include, Push Registry settings and more. For our purposes, we will use the default
project settings. Click "OK" to finish creating the project.

Mobile Application Development 9


J.E.D.I.

Copy HelloMidlet.java into the project's "src directory: In Window, this is under the
directory: C:\WTK23\apps\HelloMidlet\src (where C:\WTK23 is where you installed the
wireless toolkit). Click "Build" and "Run":

Mobile Application Development 10


J.E.D.I.

Mobile Application Development 11


J.E.D.I.

6 Using Netbeans and Mobility Pack

As a pre-requisite for this section, Netbeans 4.1 and Mobility Pack should be installed on
your computer.

Step 1: Start creating a new Project

Mobile Application Development 12


J.E.D.I.

Step 2: Select the "Mobile" Category

Step 3: Select "Mobile Application"

Mobile Application Development 13


J.E.D.I.

Step 4: Name the Project and specify its location


(Uncheck "Create Hello MIDlet, we would be creating our own MIDlet later)

Step 5: Select the Platform (optional)

Mobile Application Development 14


J.E.D.I.

Figure 1: Newly Created Mobile Project (NetBeans)

Mobile Application Development 15


J.E.D.I.

Step 6: Start creating a new MIDlet

Mobile Application Development 16


J.E.D.I.

Step 7: Choose MIDP "Category" and MIDlet "File Type"

Mobile Application Development 17


J.E.D.I.

Step 8: Create a Name for the MIDlet

Mobile Application Development 18


J.E.D.I.

Figure 2: Creating a new MIDlet automatically creates required MIDlet methods

Mobile Application Development 19


J.E.D.I.

Step 10: Replace the automatically created code with our program's code.

Mobile Application Development 20


J.E.D.I.

Step 11: Compile and Run the MIDlet in the Emulator

Mobile Application Development 21


J.E.D.I.

Step 12: Launch our MIDlet on the Emulator

Mobile Application Development 22


J.E.D.I.

Figure 3: Hello World MIDlet in action

Mobile Application Development 23


J.E.D.I.

7 Exercises
7.1 Multiple MIDlets in one MIDlet suite

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.

7.2 Multiple MIDlets in one MIDlet suite using the Wireless


Toolkit

Use the Sun Wireless Toolkit to add a new MIDlet into your MIDlet suite.

Mobile Application Development 24

You might also like