2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

Laboratory: J2ME - Introduction to Java 2 Micro Edition (J2ME)

I. Objective

To familiar with the basic components and programming issues of J2ME.

II. Architecture of J2ME

J2ME, which is developed by Sun Microsystems, is a platform, a collection of


technologies and specifications that are designed for different products of the small device
market.

A mobile phone, which lacks the computational power, memory, and workstation
power, cannot perform the same functionality as high-end servers or client workstations.
J2ME aims to port the Java programming language to devices with resource limitations. There
are various kinds of mobile phones, and it is the reason why J2ME is divided into
configurations, profiles, and optional packages. Configurations are specifications that detail a
virtual machine and a base set of APIs that can be used with a certain class of devices. A
configuration, for example, might be designed for devices that have less than 512 KB of
memory and an intermittent network connection.

Connected, Limited Device Configuration (CLDC) is for small wireless devices with
intermittent network connections, like pagers, mobile phones, and Personal Digital Assistants
(PDAs). CLDC consists of a standardized set of functionalities that all vendors who offer
J2ME-certified phones will support.

The Mobile Information Device Profile (MIDP), which is based on CLDC, was the
first finished profile and thus the first finished J2ME application environment. It is designed
for supporting the various kinds of cell phones or similar devices constrained by screen and
keypad limitations, in addition to the battery, processor, and bandwidth constraints. The
profile contains a series of APIs that let you create anything from video games using
customized graphics to full-scale business applications using external and internal data
sources.

Optional packages with CLDC, CDC, and their corresponding profiles are created to
meet different specific market requirements. Optional packages offer standard APIs for using
both existing and emerging technologies such as Bluetooth, Web services, wireless messaging,
multimedia, and database connectivity. Because optional packages are modular, device
manufacturers can include them as needed to use the features of each device.

The KVM (kilo virtual machine) and the interpreter are both contained in the CLDC.
J2ME also provides another version for high-end PDA and other mobile devices which have
more processing capability as shown in Figure 1.

1
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

Figure 1
(Source : http://java.sun.com/j2me/j2me-ds.pdf)

III. J2ME Wireless Toolkit

J2ME Wireless Toolkit 2.2 is compliant with Connected Limited Device


Configuration 1.1 (CLDC), Mobile Information Device Profile 2.0 (MIDP), Wireless
Messaging APIs (WMA), version 1.1, and Mobile Media APIs (MMAPI), version 1.1.
Support for J2ME Web Services (JSR-172), version 1.0 is also included.

The toolkit enables users to select the configurations, profiles, and optional packages
that will be used by the MIDlets applications. The APIs calls will be verified during the build
and packaging process as some of APIs may not be supported in some profiles.

JavaTM 2 SDK, Standard Edition (J2SE SDK), version 1.4.2 (current version 1.4.2_10
- http://java.sun.com/j2se/1.4.2/download.html) is required for this toolkit.

The J2ME Wireless toolkit can be downloaded at:


http://java.sun.com/products/j2mewtoolkit/index.html
(http://java.sun.com/products/sjwtoolkit/download-2_2.html)

It is totally free of charge but you need to register to the Sun Download Center first.
The registration is also free of charge.

2
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

Default directory for J2ME – c:\WTK22

Here is the description of each directory in a project.

{j2mewtk.dir}\apps\{project.name} Contains all source, resource, and binary files of


the project

{j2mewtk.dir}\apps\{project.name}\bin Contains the JAR, JAD, and unpacked manifest


files.

{j2mewtk.dir}\apps\{project.name}\lib Contains external class libraries, in JAR or ZIP


format for a specific project.

{j2mewtk.dir}\apps\{project.name}\res Contains all the resource files.

{j2mewtk.dir}\apps\{project.name}\src Contains all the source files.

{j2mewtk.dir}\apps\lib Contains external class libraries, in JAR or ZIP


format for all KToolbar projects.

Note:
JAR file is a file format based on the popular ZIP file format. It is used for aggregating many
files into one. They are commonly used as building blocks for applications and extensions. A
JAR file manifest consists of a main section followed by a list of sections for individual JAR
file entries. The main section contains security and configuration information about the JAR
file itself, as well as the application or extension that this JAR file is a part of. The individual
section defines various attributes for packages or files contained in this JAR file. Not all the
files in a JAR file need to be listed in the manifest as entries. However, all the files which are
to be signed must be listed.

(Source : http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Manifest)

3
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

IV. The first J2ME application

1. Launch the application “KToolbar” of J2ME Wireless Toolkit, as shown in below:

2. Click new project, enter dem01_howToCompile as the project name, and


cs4289.j2me2_2.helloMidlet (no .java) for the MIDlet class name. The directory
structure described in Section III will be created by the toolkit.

Notice :

Directory Structure :

4
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

3. Use your text editor to store the following codes at


H:\WTK22\apps\demo01_howtoCompile\src\cs4289\j2me2_2\helloMIDlet.java

package cs4289.j2me2_2;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class helloMidlet


extends MIDlet implements CommandListener {
private Display display_scr;
private TextBox tb_msg;
private Command cmd_exit;
private int maxChar = 40;
private int priority = 1;

/**
* constructor
*/
public helloMidlet() {
super();

/* Please refer to \docs\api\midp\index.html */

display_scr = Display.getDisplay(this);

cmd_exit = new Command("Exit", Command.EXIT, priority);

tb_msg = new TextBox("CS4289", "Welcome to this course", maxChar, TextField.


UNEDITABLE);

tb_msg.addCommand(cmd_exit);
tb_msg.setCommandListener(this);
}

/**
* Called when this MIDlet is started for the first time,
* or when it returns from paused mode.
*/
public void startApp() {
System.out.println("startApp() is called");

display_scr.setCurrent(tb_msg);
}

/**
* Called when this MIDlet is paused.
*/
public void pauseApp() {
System.out.println("pauseApp() is called");
}

5
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

/**
* Destroy must cleanup everything not handled
* by the garbage collector.
*/
public void destroyApp(boolean unconditional) {
System.out.println("destroyApp() is called");
}

public void commandAction(Command c, Displayable s) {


if (c == cmd_exit){
destroyApp(true) ;
notifyDestroyed();
}
}
}

4. Click Build (at short cut bar)

5. Click Run (at short cut bar) and show the result to your instructor.

Code explainations

Command infoCommand = ("Exit", Command.EXIT, priority);

As you can see, the Command class constructor takes three parameters. It contains the
following three pieces of information: label, type, and priority.

 The label is a string for the visual representation of the command. For example, the
label may appear next to a soft button on the device or as an element in a menu as
shown in Figure 1.

 The type specifies the command's intent. The defined types are: BACK, CANCEL,
EXIT, HELP, ITEM, OK, SCREEN, and STOP.

 The priority value describes the importance of this command relative to other
commands on the screen. A priority value of 1 indicates the most important command,
and higher priority values indicate commands of lesser importance.

The documentation of API can be found at the directory of the wireless toolkit.
({j2mewtk.dir}\docs\api\midp\index.html)

6
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

V. Laboratory Task (High level GUI )

Modify the program to make it to accept user information and show all information
after pressing the “Confirm” command.

You need to create one more TextField(s) for accepting user information.

Note: The J2ME documentation is located at the following directory. You may use
them for references.

H:\WTK22\docs\api\midp\index.html

Hints : You need to refer to the following classes to complete lab task I

Class Display
Class Screen (Abstract Class)
Class Ticker
Class Form
Class Alert
Class TextField
Class Command

7
J2ME Laboratory Sheet CS4289/CS5289 Projects in Pervasive Computing

References:

1. Kim Topley, “J2ME in a Nutshell”, O’Reilly, 2002

2. http://java.sun.com

You might also like