Lecture Slides

Download Report

Transcript Lecture Slides

J2ME
Java for Mobile Environments
Based on J2ME In A Nutshell, by Kim Topley,
O’Reilly & Associates Inc., 2002,
and Tetris by Alexei Patinov.
What is J2ME?

J2ME : Java 2 Micro Edition
 J2ME defines two configurations, ranges of hardware for
which it can generate software:
• CLDC : Connected Limited Device Configuration
– CLDC devices include cell phones, PDAs, other items with
roughly up to 512Kb of RAM.
– CLDC devices have very limited processors; for example,
CLDC devices are not required to support double or float
types.
• CDC : Connected Device Configuration
– CDC devices fill the gap between PDAs and desktops;
typical CDC devices might include Psions, smart phones,
and set-top web devices.
 Choice of Virtual machine is up to the hardware vendor.
 Many CLDC device vendors use Sun’s KVM, the Kilobyte
Virtual Machine.
J2ME : Java 2 Micro Edition

Profiles
 J2ME complements configurations with profiles, software to
match the hardware.
 Each configuration supports several profiles.
 The most common profile in mobile phones is MIDP, the
Mobile Information Device Protocol, which adds networking,
user interface, and persistent storage support to the CLDC
configuration.

J2SE re-use
 J2ME is designed to re-use J2SE code wherever possible, or
at least to pretend to.
 Within the scope of features supported by a given
configuration and set of profiles, wherever possible the J2ME
implementation must not change interfaces or behavior from
the J2SE original.
J2ME : Java 2 Micro Edition

CLDC hardware requirements
 128 Kb permanent storage for the J2ME engine and classes
 32 Kb of RAM for applications to run in

MIDP hardware requirements
 A screen at least 96 pixels wide and 54 pixels tall

Things the CLDC does not require:




Keyboard
Mouse
Floating-point support
Persistent app data storage
(This is supported by the MIDP profile.)
 Multithreading or multiprocessing
(This is not required of the hardware; it’s left to the JVM.)
J2ME : Java 2 Micro Edition

J2SE features missing from the CLDC:
 Reflection (java.lang.reflect)
 Weak references (java.lang.ref)
 Object finalization (java.lang.Object has no finalize() method)

J2SE features reduced in the CLDC:
 Advanced thread handling
 Advanced error handling
 JNI (the Java Native Interface)

J2SE behavior changed in the CLDC:
 A number of security restrictions
 The class verification step
(J2SE verifies bytecode on load. J2ME shifts the bytecode
verification step to the vendor’s compiler.)
MIDP Software Development

MIDP applications are called MIDlets.
 (Because everything in Java is a somethinglet.)
 MIDlets are groups of classes, one of which is derrived from
the abstract class javax.microedition.midlet.MIDlet.

MIDlets can be packaged in MIDlet suites to
group one or more MIDlets into the same space.
 MIDlets sharing a suite can access each other’s persistent
data and are run concurrently, giving an application-level
interface to multithreading.
 MIDlets sharing a suite are installed and uninstalled together.
 Classes defined in a MIDlet suite are shared between all
concurrent midlets; only one instance of each class in the
suite is loaded per suite.
MIDP Software Development

MIDlet packaging and installation
 Deploying a MIDlet requires several more steps than
deploying a conventional application.

JAR files and JAD files
 The MIDlet [suite] being deployed is packaged, along with all
of its supporting classes and data files, into a JAR file.
 The JAR also needs to include a manifest which will detail the
contents of the JAR.
 The JAR must be paired with a JAD (Java Application
Descriptor) file for installation.
 The .jad is almost identical to the JAR’s manifest.mf; where
the manifest is to allow installed software to be described to
the user after loading, the .jad is used to describe the
application to the user before it’s downloaded as part of the
installation process.
MIDP Software Development

Sample .jad or manifest.mf :
MIDlet-1: Tetris,, TetrisMIDlet
MIDlet-Description: Tetris MIDlet demo
MIDlet-Name: Tetris
MIDlet-Vendor: Me
MIDlet-Version: 1.0.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDP Software Development

Software you’ll need to install in order to develop
MIDP applications:
 The Java 2 SDK
• I use J2SDK version 1.4.1.01
 The J2ME CLDC
 Sun’s Wireless Toolkit 1.04
 At least one cell phone emulator
• I use the Nokia 7210 MIDP SDK 1.0 emulator
 Software to deploy to your phone
• I use the Nokia Developer Suite
MIDP Software Development

Compiling and deploying a MIDlet
1) Java compiler
 javac.exe
-target 1.1
-bootclasspath "[Wireless toolkit path]\lib\midpapi.zip"
-d preverify
*.java
2) Preverify
 preverify.exe
-classpath "[Wireless toolkit path]\lib\midpapi.zip"
-cldc
-d postverify .\preverify
3) JAR builder
 jar.exe cvfm Project.jar Project.jad -C postverify .
4) Deploy and test
MIDP Software Deployment

Deploying MIDlets over the WWW
 After you’ve tested your MIDlet on your local emulator and on
a locally-connected cell phone (perhaps connected over an IR
port) you’ll want to ship it to the world at large.
 You’ll need to configure your web server to support the JAD
MIME type. The JAD MIME type is
text/vnd.sun.j2me.app-descriptor
 The user can now click on an HTML link such as
Click <a href=“sample.jad”>here</a> to...
 This will download the JAD file. An attribute in the JAD file is
the MIDlet-Jar-URL; if the phone user chooses to continue,
the phone requests this URL from the server.
 The JAR file is then transmitted with MIME type
application/java-archive
Anatomy of a MIDlet


A MIDlet consists of [at least] one class
extending javax.microedition.midlet.MIDlet, plus
supporting classes.
You must implement three abstract methods:
 startApp()
 pauseApp()
 destroyApp(boolean unconditional)

In startApp() you’ll create a user interface object-a Canvas or Screen.
Anatomy of a MIDlet

J2ME provides two user interface approaches:
 High-level UI: Screen
The Screen class and those derrived from it provide support
for an assisted layout of high-level objects like buttons
and text fields. This means that while it’s easier to control
what goes where and how events are passed and so on,
it’s harder to control precisely the contents of the screen.
Derived classes: Alert, Form, List and TextBox.
 Low-level UI: Canvas
The Canvas class gives the developer complete control over
the contents of the screen. While this has the upshot of
giving you advanced graphical abilities and precise
screen control, it has the downside that you have to do
everything yourself. Tetris was written on a Canvas.
Anatomy of a MIDlet

“Hello World”, the MIDlet:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloWorldMIDlet extends MIDlet
{
public HelloWorldMIDlet()
public void pauseApp()
public void destroyApp(boolean unconditional)
{ }
{ }
{ }
public void startApp()
{
if (Display.getDisplay(this).getCurrent() == null)
{
HelloScreen helloScreen = new HelloScreen(this, "Hello World.");
Display.getDisplay(this).setCurrent(helloScreen);
}
}
void exitRequested()
{
destroyApp(false);
notifyDestroyed();
}
}
Anatomy of a MIDlet

“Hello World” screen class:
import javax.microedition.lcdui.*;
class HelloScreen
extends TextBox
implements CommandListener
{
private final HelloWorldMIDlet m_midlet;
private final Command
m_exitCommand;
HelloScreen(HelloWorldMIDlet midlet, String string)
{
super("HelloWorldMIDlet", string, 256, 0);
m_midlet = midlet;
m_exitCommand = new Command("Exit", Command.EXIT, 1);
addCommand(m_exitCommand);
setCommandListener(this);
}
public void commandAction(Command c, Displayable d)
{
if (c == m_exitCommand)
m_midlet.exitRequested();
}
}
Recommended Reading

J2ME In A Nutshell, by Kim Topley, O’Reilly &
Associates Inc., 2002
 Dense yet informative; details the guts of J2ME from below
CLDC on up to forms and timers. Also has a handy reference
to the new J2ME Java classes.

http://java.sun.com
 The resource of all things Java.
 This is where you can download the J2SDK and Wireless
Toolkit.

http://forum.nokia.com
 Good site to download phone emulators and SDKs.
 Non-intrusive signup process required