Tutorial 1

weaverchurchSoftware and s/w Development

Aug 15, 2012 (4 years and 10 months ago)

237 views

Worksheet 1


Start the J2ME Wireless toolkit by selecting Ktoolbar. This displays a simple
interface.

1.

Open a sample project. Select settings to view the file size and other
information


note the jar file, midp version etc. You will not change these but
t
he information should now make sense


if not look again at the lecture notes

2.

Select Build so that the class files and packages are generated. Then select run.

3.

Note the use of an emulator to provide the typical phone display



4.

Launch Windows explorer
and locate the chosen project directory. Study the
files associated with the project.



5.

Using NetBeans

develop a MIDlet of your own


a quick way to do this would
be to copy across some sample Midlet such as the one you have just executed
and edit the code

as described below. NetBeans includes documentation to
show the mobile development process. You should familiarise yourself with
this.

We will be using the Sun/Java J2ME SDK. This SDK includes the J2ME Wireless
Toolkit. Here's its GUI (called K Toolbar):


Creating a New Project and Midlet

Applications that run on a MIDP/CLDC platform are called
midlets
. By pressing the
"New Project" button we create a new project named
Examples

containing a midlet
defined in a file named
HelloWorld.class
:


The console wi
ndow informs us that it has created a project directory named
Examples

containing
src
,
res
, and
lib

subdirectories:


Other subdirectories have been created as well. We must save our
.java

files in the
Examples
\
src

subdirectory.

HelloWorld.java

The file
Ex
amples
\
src
\
HelloWorld.java

is created using an ordinary text editor



or
JBuilder
. It begins by importing several J2ME packages:

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

The declaration of the HelloWorld class follows:

public
class HelloWorld
extends MIDlet

{


// Device's display window and main screen:


private Display theDisplay;


private Screen mainScreen;


public HelloWorld() {



theDisplay = Display.getDisplay(this);



mainScreen = new TextBox(




"Example 1", "Hello World
", 50, TextField.ANY);


}


// Lifecycle methods:


protected void startApp() {



theDisplay.setCurrent(mainScreen);


}


protected void destroyApp(boolean unconditional) { }


protected void pauseApp() { }

}

Notice that HelloWorld extends MIDlet. Two instance

variables are declared and
subsequently initialized by the constructor.
theDisplay

represents the display window of
the PDA or mobile phone.
mainScreen

represents the screen that
theDisplay

is supposed
to display. This is accomplished in
startApp()

method

by the line:

theDisplay.setCurrent(mainScreen);

startApp() is one of three lifecycle methods every midlet must define. In most of our
examples the other two have empty bodies. We can think of
theDisplay

as a mini web
browser and
mainWindow

as a type of we
b page. In our example,
mainPage

is a text
box titled "Example 1" and containing the text "Hello World".

Running HelloWorld

Pressing the Build button on the toolbar causes the Wireless Toolkit to compile all

.java
files in the current project's
src

subdire
ctory. The corresponding
.class

files are placed
in the
classes

subdirectory. Next, the
.class

files are pre
-
verified. If a
.class

file passes
various security checks, it is tagged as verified. All of the project's verified files are
placed in an archive f
ile called
Examples.jad
. This file is placed in the
bin

subdirectory.

Pressing the Run button on the toolbar causes the Wireless Toolkit to start a mobile
phone simulator. The
Examples

application suite is "downloaded" to the "phone," which
displays the s
uite's midlets in its window. Currently, HelloWorld is the only midlet.
Launching this midlet causes the window to display the midlet's main page. Use the red
"hang up" button to terminate the midlet.


We shall now edit the MIDlet providing our own display

to illustrate the high level
GUI API. This provides basic screen interaction with the user:


Replace the MIDlet code with the following members, add other functions as listed
below, then compile and execute the code.


// Place the following members in the

MIDlet class

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;


public class HelloWorld extends MIDlet implements CommandListener{



private final static String a1 = "HelloWorld";


private final static String a2 = "Bye";


private C
ommand exitCommand;


private Command backCommand;


private Display display;


private TextBox t;



public HelloWorld() {


display = Display.getDisplay(this);


exitCommand = new Command("Exit", Command.EXIT, 1);


backCommand = new Command("Back"
, Command.BACK, 1);


}



public void startApp()


{


List l = new List("Choose", Choice.IMPLICIT);


l.append(a1, null);


l.append(a2, null);


l.addCommand(exitCommand);


l.setCommandListener(this);


display.setCurrent(l);


}



public
void helloWorld()


{


t = new TextBox(a1, "Hello World", 256, TextField.ANY);


t.addCommand(exitCommand);


t.addCommand(backCommand);


t.setCommandListener(this);


display.setCurrent(t);


}



public void commandAction(Command c, Displayab
le s)


{


if (c == List.SELECT_COMMAND)


{


List l = (List) s;


String choice = l.getString(l.getSelectedIndex());


if (choice.equals(a1))


{


helloWorld();


}



else if (choice.equals(a2))


{


notify
Destroyed();


}


}


int commandType = c.getCommandType();


if (commandType == Command.BACK)


startApp();


else if (commandType == Command.EXIT)


notifyDestroyed();




}




protected void destroyApp(boolean uncondi
tional) {}


protected void pauseApp() {}



}