Sie sind auf Seite 1von 18

J.E.D.I.

Getting Started with Mobile


Programming

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.

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

public class HelloMidlet extends MIDlet implements CommandListener {

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


program will 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){
destroyApp(true);
notifyDestroyed(); // Exit

Mobile Application Development 4


J.E.D.I.

}
}

Our commandAction() only handles request for the "Exit" command. Issuing or pressing
the "Exit" command will terminate our program using notifyDestroyed().

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 is 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 and run our first MIDlet.

Mobile Application Development 5


J.E.D.I.

4 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 6


J.E.D.I.

Step 2: Select the "Mobile" Category

Step 3: Select "Mobile Application"

Mobile Application Development 7


J.E.D.I.

Step 4: Name the Project and specify its location


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

Step 5: Select the Platform (optional)

Mobile Application Development 8


J.E.D.I.

Figure 1: Newly Created Mobile Project (NetBeans)

Mobile Application Development 9


J.E.D.I.

Step 6: Start creating a new MIDlet

Mobile Application Development 10


J.E.D.I.

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

Mobile Application Development 11


J.E.D.I.

Step 8: Create a Name for the MIDlet

Mobile Application Development 12


J.E.D.I.

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

Mobile Application Development 13


J.E.D.I.

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

Mobile Application Development 14


J.E.D.I.

Step 11: Compile and Run the MIDlet in the Emulator

Mobile Application Development 15


J.E.D.I.

Step 12: Launch our MIDlet on the Emulator

Mobile Application Development 16


J.E.D.I.

Figure 3: Hello World MIDlet in action

Mobile Application Development 17


J.E.D.I.

5 Exercises

5.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.

Mobile Application Development 18

Das könnte Ihnen auch gefallen