0% found this document useful (0 votes)
44 views

CH 02

The document provides instructions for creating a simple "Hello World" mobile application using Java ME. It discusses: 1) Creating a subclass of the MIDlet class that implements the required lifecycle methods like startApp(), pauseApp(), and destroyApp(). 2) Initializing an Alert object in the constructor to display text, adding an exit command, and setting a command listener. 3) Implementing the commandAction() method to handle the exit command by destroying the app. 4) Displaying the Alert in startApp() by getting the display and setting the current displayable. 5) Compiling, preverifying, adding to a JAR file with a manifest, and using

Uploaded by

Rian Mags
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

CH 02

The document provides instructions for creating a simple "Hello World" mobile application using Java ME. It discusses: 1) Creating a subclass of the MIDlet class that implements the required lifecycle methods like startApp(), pauseApp(), and destroyApp(). 2) Initializing an Alert object in the constructor to display text, adding an exit command, and setting a command listener. 3) Implementing the commandAction() method to handle the exit command by destroying the app. 4) Displaying the Alert in startApp() by getting the display and setting the current displayable. 5) Compiling, preverifying, adding to a JAR file with a manifest, and using

Uploaded by

Rian Mags
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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

J.E.N.I.

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

Pengembangan Perangkat Mobile

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);
}

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
}

Pengembangan Perangkat Mobile

J.E.N.I.

}
}

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.

Pengembangan Perangkat Mobile

J.E.N.I.

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

Pengembangan Perangkat Mobile

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 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)

Pengembangan Perangkat Mobile

J.E.N.I.

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)

Pengembangan Perangkat Mobile

J.E.N.I.

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.

Pengembangan Perangkat Mobile

J.E.N.I.

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

J.E.N.I.

Pengembangan Perangkat Mobile

10

J.E.N.I.

Pengembangan Perangkat Mobile

11

J.E.N.I.

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

Pengembangan Perangkat Mobile

12

J.E.N.I.

Step 2: Choose the category "Mobile"

Pengembangan Perangkat Mobile

13

J.E.N.I.

Step 3: Choose the "Mobile Application"

Pengembangan Perangkat Mobile

14

J.E.N.I.

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)

Pengembangan Perangkat Mobile

15

J.E.N.I.

Figure 1: newly created Mobile Project (NetBeans Mobility Pack)

Pengembangan Perangkat Mobile

16

J.E.N.I.

Step 6: Create a new MIDlet

Pengembangan Perangkat Mobile

17

J.E.N.I.

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

Pengembangan Perangkat Mobile

18

J.E.N.I.

Step 8: Name the MIDlet

Pengembangan Perangkat Mobile

19

J.E.N.I.

Step 9

Figure 2: Creating a MIDlet. MIDlet automatically create the required method

Pengembangan Perangkat Mobile

20

J.E.N.I.

Step 10: Replace the code that is created automatically by our program code.

Pengembangan Perangkat Mobile

21

J.E.N.I.

Step 11: Compile and Run (Run) MIDlet in Emulator

Pengembangan Perangkat Mobile

22

J.E.N.I.

Step 12: Running MIDlet us in Emulator

Pengembangan Perangkat Mobile

23

J.E.N.I.

Figure 3: Hello World MIDlet

Pengembangan Perangkat Mobile

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.

2.7.2 Multiple MIDlets in the MIDlet suite using Wireless Toolkit


Use the Sun Wireless Toolkit to add a new MIDlet in the MIDlet application you.

Pengembangan Perangkat Mobile

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy