Vous êtes sur la page 1sur 44

Introduction to J2ME

ESPRIT Saif Braham


saif.braham@esprit.ens.tn
Java 2 Platform Micro Edition
(J2ME)
 Java platform for small devices
 A subset of J2SE
 Released mid June 1999
 Target devices:
– Two-way pagers
– Mobile phones, smart phones
– PDAs (inc PocketPCs)
– TVs, VCRs, CD players
 Almost every mobile phone support J2ME

Saif.Braham@esprit.ens.tn Introduction to J2ME 2


J2ME Phones

Saif.Braham@esprit.ens.tn Introduction to J2ME 3


J2ME Phones (Up to 15 Jan 2008)
http://developers.sun.com/mobility/device/pub/device/list.do?sort=manufacturer&filterIds=61&page=1

Saif.Braham@esprit.ens.tn Introduction to J2ME 4


3 Java Platforms

Java 2 Platform

Java2 Java2 Java2


Standard Edition Enterprise Edition Micro Edition
(J2SE) (J2EE) (J2ME)
Standard desktop & Heavy duty server Small & memory
Workstation Applications systems Constrained devices

Saif.Braham@esprit.ens.tn Introduction to J2ME 5


Saif.Braham@esprit.ens.tn Introduction to J2ME 6
J2ME Architecture
 To increase the flexibility of design, the J2ME
consists of two distinct layers:
Configurations and Profiles

 Configuration
– Defines the minimum Java technology for a broad
range of devices with similar capabilities

 Profile
– Provides capabilities, on top of configuration, for a
specific device type

Saif.Braham@esprit.ens.tn Introduction to J2ME 7


J2ME Architecture
 Two types of J2ME
configurations J2ME Profile
Profile
1. Connected Device
Configuration
2. Connected Limited
Device Configuration
Configuration
J2ME
Libraries CDC, or
CLDC

Java
Virtual Machine

Saif.Braham@esprit.ens.tn Introduction to J2ME 8


CLDC vs CDC
 CLDC
160 Kbytes to 512 Kbytes of total memory
available
16-bit or 32-bit processor
Low power consumption and often operating
with battery power
Connectivity with limited bandwidth.
 CDC
2Mbytes or more memory for Java platform
32-bit processor
High bandwidth network connection, most
often using TCP/IP

Saif.Braham@esprit.ens.tn Introduction to J2ME 9


CLDC

Saif.Braham@esprit.ens.tn Introduction to J2ME 10


Mobile Information Device Profile
(MIDP)
 Is a set of APIs that allow developers to control
mobile device-specific problems
– i.e. user interfaces, local storage and client
application lifecycles etc.
 MIDlets minimum requirements
– 96 x 54 pixels mono screen
– two-way wireless network
– input device (i.e. keypad)
– 128 KB for CLDC/MIDP class and another 32 KB
for the KVM
 Midlets are the most important and popular
applications in the J2ME family.

Saif.Braham@esprit.ens.tn Introduction to J2ME 11


MIDP

Saif.Braham@esprit.ens.tn Introduction to J2ME 12


Building J2ME Apps- Tool
 We will use Sun Java Wireless Toolkit 2.x for
CLDC (The newest version is 2.5.2 in Jan 2008)
which can be downloaded from
http://java.sun.com/j2me/download.html

Saif.Braham@esprit.ens.tn Introduction to J2ME 13


J2ME Wireless Toolkit Demo
 Launch the Wireless Toolkit:
– Start > Programs > Sun Java(TM) Wireless Toolkit
2.5.2 for CLDC

 WTK already includes a set of demo programs ready


to run.

Saif.Braham@esprit.ens.tn Introduction to J2ME 14


J2ME Wireless Toolkit Demo
 Select menu item
File > Open Project ...
 Select UIDemo and
click Open Project.

 The projects can be used as the templates of your


applications.
Saif.Braham@esprit.ens.tn Introduction to J2ME 15
J2ME Wireless Toolkit Demo
 Click the Build and then the Run buttons.

Saif.Braham@esprit.ens.tn Introduction to J2ME 16


J2ME Wireless Toolkit Demo
 The main menu screen is shown up. You can choose
a program and select Launch to start the program.

Saif.Braham@esprit.ens.tn Introduction to J2ME 17


MIDlet Programming
 Any MIDP application must extends MIDlet
 This is the MIDP equivalent of an applet, where
starting/stopping is under the control of the
environment
 Like Java applets, MIDlets have an application life
cycle while running on a mobile device.

Saif.Braham@esprit.ens.tn Introduction to J2ME 18


MIDlet Transition States
 Specifically, a MIDlet can be in one of three states as
shown:

Why do we need
a Paused state?

Saif.Braham@esprit.ens.tn Introduction to J2ME 19


Midlet Skeleton
import javax.microedition.midlet.*; Note that startApp(), pauseApp()
import javax.microedition.lcdui.*; and destroyApp() are abstract
methods.
public class MyApp extends MIDlet {
public void startApp() { You Midlet program must
// start up code override these 3 methods even
} though you are not do anything
in it.
public void pauseApp() {
// we aren't showing any more
}

public void destroyApp(boolean unconditional) {


// clean up
}
}

Saif.Braham@esprit.ens.tn Introduction to J2ME 20


Two Level API
 There are two areas the API which you should be
concerned with - the high and low-level API.
 High-Level Provides input elements such as,
– text fields, choices, and form
 Low-level is for drawing on Canvases and capturing
keyed events
 All MIDlet applications need to import the necessary
midlet and lcdui packages:
– import javax.microedition.midlet.*;
– import javax.microedition.lcdui.*;

Saif.Braham@esprit.ens.tn Introduction to J2ME 21


Displaying Objects
 High-level Screens have a base class called
Displayable.
 To show something on a MIDP device, you need to
obtain the device’s display
– javax.microedition.lcdui.Display class.
 This Display class is the one and only display
manager for each active MIDlet and provides
information about the device’s display capability.
 Subclassed Displayable classes will fill the whole
screen

Saif.Braham@esprit.ens.tn Introduction to J2ME 22


Displaying Objects
 To show a Displable object you must use the
setCurrent() method on the Display object.
Form mainForm = new Form ("First Program ");
Display display = Display.getDisplay(this);
display.setCurrent (mainForm);

Note that Form is a Displayable subclass.

Saif.Braham@esprit.ens.tn Introduction to J2ME 23


First Example - HelloWorld
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet {

public HelloWorld() {
}

public void startApp() {

Form form = new Form( "First Program" );


form.append( "Hello World" );
Display.getDisplay(this).setCurrent( form );
}

public void pauseApp() {


}

public void destroyApp( boolean unconditional ) {


}
}
Saif.Braham@esprit.ens.tn Introduction to J2ME 24
Building the MIDlet
 Run the program KToolbar
Start>Programs>J2ME Wireless Toolkit 2.x>KToolbar
 Click on New Project and enter the Project name and
class name as shown below

Saif.Braham@esprit.ens.tn Introduction to J2ME 25


Building the MIDlet
 After pressing the Create Project Button, a directory
tree will be created for the project:

Saif.Braham@esprit.ens.tn Introduction to J2ME 26


Building the MIDlet
 Use TextPad to create a source file HelloWorld.java
and save it under the directory src.

Saif.Braham@esprit.ens.tn Introduction to J2ME 27


Building and Run the MIDlet
 Click the Build and then the Run buttons.

Saif.Braham@esprit.ens.tn Introduction to J2ME 28


How can the program exit?
 The program can not exit unless you close the
emulator.
 To provide a way to exit the program, you need to
use Commands.
 A command is like a button, it has a title, like "OK" or
"Cancel," and your application can respond
appropriately when the user invokes the command.

Saif.Braham@esprit.ens.tn Introduction to J2ME 29


Event Handling with Commands
 Displayable, the parent of all screen displays,
supports Commands.
 The device determines how the commands are
shown on the screen or invoked by user.
 Every Displayable keeps a list of its Commands. You
can add and remove Commands using the following
methods:
– public void addCommand(Command cmd)
– public void removeCommand(Command cmd)

Saif.Braham@esprit.ens.tn Introduction to J2ME 30


Command Objects
 In J2ME, commands are commonly represented with
soft-buttons on the device. The following diagram
shows two Command objects, one with the label
"Exit" and one with label "View."

soft-buttons

Saif.Braham@esprit.ens.tn Introduction to J2ME 31


Command Objects
 If there are too many commands to be shown on the
display, a device will create a menu to hold multiple
commands. The following diagram shows how this
might look.

Saif.Braham@esprit.ens.tn Introduction to J2ME 32


Use Command objects
 The basic steps to process events with a Command
object are as follows:
1. Create a Command object.
2. Add the Command to a Form (or other GUI
objects TextBox, List, or Canvas).
3. Create and set a listener for the Form.
 Upon detection of an event, the listener will call the
method commandAction().

Saif.Braham@esprit.ens.tn Introduction to J2ME 33


Create a Command
 To create a Command, you need to supply a label, a
type, and a priority.
 The type is used to signify a commonly used
command. It helps device to arrange the commands.
Command Meaning

BACK returns to the previous screen.


CANCEL standard negative answer to a dialog
EXIT for exiting from the application.
HELP a request for on-line help.
ITEM specific to the items of the Screen or the elements of a
Choice.
OK standard positive answer to a dialog
SCREEN an application-defined command
STOP A command that will stop some currently running
process, operation, etc.
Saif.Braham@esprit.ens.tn Introduction to J2ME 34
Create a Command
 To create a standard OK command, for example, you
would do this:
Command c = new Command("OK", Command.OK, 0);
label
type priority

 To create a command specific to your application,


you might do this:
Command c = new Command(
"Launch", Command.SCREEN, 0);

Saif.Braham@esprit.ens.tn Introduction to J2ME 35


Priority and Long Label
 Every command has a priority.
 Lower numbers indicate a higher priority.
 If you add a command with priority 0, then several
more with priority 1, the priority 0 command will show
up on the screen directly. The other commands will
most likely end up in a secondary menu.
 MIDP also supports for long labels on commands.
 You can create a command with a short and long
label like this:
Command c = new Command("Run", "Run simulation",
Command.SCREEN, 0);
 The device decides which label it will use based on
the available screen space and the size of the labels.
Saif.Braham@esprit.ens.tn Introduction to J2ME 36
Responding to Commands
 Commands show up on the screen, but nothing
happens automatically when a user invokes a
command.
 You need to write an object called a listener which
will be called when the user invokes any command in
a Displayable.
 The listener is an object that implements the
CommandListener interface.
 To register the listener with a Displayable, use the
following method:
– public void setListener(CommandListener l)
 Note it is one Listener per Displayable, NOT one
Listener per one Command.

Saif.Braham@esprit.ens.tn Introduction to J2ME 37


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

public class Commander extends MIDlet implements CommandListener {


public void startApp() {
Displayable d = new Form( "Test Command" );
Command c = new Command("Exit", Command.EXIT, 0);
d.addCommand(c);
d.setCommandListener(this);
Display.getDisplay(this).setCurrent(d);
}

public void pauseApp() { }


public void destroyApp(boolean unconditional) { }

public void commandAction(Command c, Displayable s) {


notifyDestroyed();
}
} Abstract method of CommandListener. Will
be called when any command in the Form is
selected.
Saif.Braham@esprit.ens.tn Introduction to J2ME 38
Saif.Braham@esprit.ens.tn Introduction to J2ME 39
Another Command Example
(Two Forms)
Launch

Exit

2nd Form

Exit
Go to First Form
Saif.Braham@esprit.ens.tn Introduction to J2ME 40
Another Command Example (Two
Forms)
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class Commander2 extends MIDlet implements CommandListener {


Display display = null;
Form f1 = null;
Form f2 = null;

// command
Command firstFormCommand =
new Command("1st Form", "Go to First Form", Command.SCREEN, 0);
Command secondFormCommand =
new Command("2nd Form", "Go to Second Form", Command.SCREEN, 0);
Command exitCommand =
new Command("Exit", Command.EXIT, 1);

Saif.Braham@esprit.ens.tn Introduction to J2ME 41


Another Command Example (Two
Forms)
public void startApp() {
display = Display.getDisplay(this);

f1 = new Form( "Form 1" );


f1.append( "This is Form No. 1" );
f1.addCommand(secondFormCommand);
f1.addCommand(exitCommand);
f1.setCommandListener(this);

f2 = new Form( "Form 2" );


f2.append( "This is Form No. 2" );
f2.addCommand(firstFormCommand);
f2.addCommand(exitCommand);
f2.setCommandListener(this);

display.setCurrent( f1 );
}

Saif.Braham@esprit.ens.tn Introduction to J2ME 42


Another Command Example (Two
Forms)
public void pauseApp() {
}

public void destroyApp(boolean unconditional) {


}

public void commandAction(Command c, Displayable d) {


String label = c.getLabel();
if (label.equals("Exit")) {
notifyDestroyed();
} else if (label.equals("1st Form")) {
Display.getDisplay(this).setCurrent( f1 );
} else {
Display.getDisplay(this).setCurrent( f2 );
}
}
}

Introduction to J2ME 43
Simple Debugging
 System.out.print and System.out.println can be used
for debugging.
 When run in the simulator, the output is put on the
console, not the phone.
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
notifyDestroyed();
} else if (label.equals("1st Form")) {
System.out.println("1st Form is called");
display.setCurrent( f1 );
} else {
System.out.println("2nd Form is called");
display.setCurrent( f2 );
}

Introduction to J2ME 44

Vous aimerez peut-être aussi