You are on page 1of 54

J2ME

Sara El-Sayed El-Metwally


Java Technology Editions
Java Technology Editions
Java Technology Editions
?What is J2ME for
 Writing Programs that run on the phone

 no need to be connected online


 uses processor and interface of phone
 Applications
 Games


for fun, but can be educational
 Calculation / Computation tools


e.g., medical calculators, etc.
 Provide a better user interface to a service

 e.g., Maps for mobile (Google, Microsoft, et al)


can be useful for data forms
?What J2ME lets you do
 Computation / logic
 general-purpose language
 limited only by processor speed and memory
 Graphics
 including 3D on new phones
 Connectivity
 SMS, MMS, Bluetooth, HTTP
 Other Features
 NFC/RFID, Location, etc.
J2ME Components
J2ME
Components

Optional
Packages

Profiles

Configurations
Notes
 Unlike the desktop and server worlds targeted by J2SE
and J2EE, the micro-world includes such a wide range of
devices with vastly different capabilities that it is not
possible to create a single software product to suit all of
them.
 Instead of being a single entity, therefore, J2ME is a
collection of specifications that define a set of a
platforms, each of which is suitable for a subset of the
total collection of consumer devices that that fall within
its scope.
 The subset of the full Java programming environment for
a particular device is defined by one or more profiles,
which extend the basic capabilities of a configuration.
Configurations

 Specific to a family of devices


i.e. Common features of a device: CPU, RAM, ROM, etc.

 Comprised of
 Virtual Machine
 Core libraries
 Classes, APIs
Configurations

Configuration

CLDC CDC

Connected Limited Connected Device


Device Configuration Configuration
Mobile Application
Configuration
Profiles
 A profile complements a configuration by
adding additional classes that provide features
appropriate to a particular type of device or to
a specific vertical market segment.

 Most profiles define user interface classes for


building interactive applications.
Profiles

Profile

MIDP PDAP FP PBP PP

Mobile
Personal Foundation Personal Personal
Information
Digital Profile Basis Profile Profile
Device
Assistant
Profile
Profile
Mobile Application
Profile
MIDP
 Mobile Information Device Profile (MIDP) defines API’s for user interface
components, input & event handling, persistent storage, networking & timers, allowing for screen &
memory limitation of mobile devices
 Minimal characteristics of MIDs:
 Enough memory to run MIDP applications,
 Display of at least 96 X 56 pixels, either monochrome or color
 A keypad, keyboard, or touch screen
 Two-way wireless networking capability
Optional Packages
 MM API – mobile media supports audio/video
controls, streaming media
 WMA API – wireless messaging, SMS and MMS
(multimedia messaging systems)
 Location API – tracking of wireless devices
 J2ME Web Services API – web services related XML
processing
 Bluetooth API – Bluetooth communications protocol
 Security and Trust API – interaction with security
smart card
Notes

 Visit developer sites


for device specifications
J2ME Stack
Mobile Application Stack
MIDlets

 A Java program created for Mobile


Devices
 MIDlets will (should) run on any
device that implements J2ME MIDP
 Like all Java programs, MIDlets are
"compile once, run anywhere“
 MIDlet = .jar file
MIDlets
 A MIDlet needs to have the following
functions in order to compile:

 startApp() – this runs when the MIDlet is started


 pauseApp() – this runs when the application is
paused e.g. incoming call
 destroyApp() – this runs when the MIDlet /
application is closed
Enjoy with MIDlets
 Import the packages
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 All MIDlet extends from the MIDlet class

public class HelloMidlet extends MIDlet


{}

MIDlet must implement three methods:

public void startApp() {}


public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
Enjoy with MIDlets

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

public class HelloMidlet extends MIDlet {


public void startApp() {
}

public void pauseApp() {


}

public void destroyApp(boolean unconditional) {


}
}
Enjoy with MIDlets
 The device’s display is represented by an
object of the Display class
Display myDisplay= Display.getDisplay(this);
 Objects that can be added to a Display are
subclasses of Displayable
 A Displayable is not visible to the user until it
is associated with the MIDlet's Display object
using the Display's setCurrent( ) method:
myDisplay.setCurrent(myDisplayable);
Enjoy with MIDlets

Display
Displayable
The Displayable Hierarchy

• All these classes are defined in


TextBox

 The TextBox is created using its only


constructor:
public TextBox(
String title,
String text,
int maxSize,
int constraints)
TextBox
 The title argument sets the title that appears
above the TextBox; you can set it to null if no title
is required.
 The second argument specifies the text that will
initially be displayed in the TextBox
 maxSize Specifies the maximum number of
characters that the TextBox can contain at any
time.
 Constraints Specifies the type of content that
should be allowed in the TextBox. Using this
argument, you can, for example, restrict the user
to entering only numbers or more complex things
such as phone numbers or URLs
TextBox Constraints
Commands

 A command is something the user can invoke


 The Command class has a single constructor:
public Command(
String label,
int type,
int priority);
Commands

 The label argument supplies the text that will


be used to represent the Command in the user
interface,
 the type and priority arguments are hints
that the MIDP implementation can use when
deciding where the Command will be placed.
The type and priority arguments are required
because of the diversity of the devices on
which MIDP is intended to be used.
Commands
 MIDlets can use the type and priority constructor
arguments to supply hints to the MIDP implementation
regarding the semantic meanings of Commands and
their relative importance, so that those likely to be most
frequently used can be made most easily accessible to
the user.

 MIDP platforms are allowed to follow their own rules


when determining how to represent Commands in the
user interface. In general, however, the choice is made
first based on the Command type and then on the
priority, where lower priority values tend to result in a
more favorable placement.
Command Types
Commands

 Example:
 Command c = new Command(“OK”,

Command.OK, 0);

 You can add commands to a Displayable using:


 Displayable. addCommand(Command);
Commands

Commands
Responding to user activation
of Commands

 When a Command is invoked by the user, a


method is called to service the command
 The exact method is:
public void commandAction(Command c,Displayable d)
 c is the Command invoked and d is the
Displayable the Command was added
to.
Responding to user activation
of Commands
 In order to be notified when the user activates a
Command, you have to register a CommandListener
with the Displayable to which the Command
was added. You do this by invoking its
setCommandListener() method:
public void setCommandListener(
CommandListener l
);
• CommandListener is an interface with
a single method: commandAction
Responding to user activation
of Commands
 Have MIDlet class to implement
CommandListener and assign the MIDlet as the
CommandListener for the Displayable
Displayable.setCommandListener(This)

MIDlet
Commands: Summary

import javax.microedition.midlet.*; MIDlet class


import javax.microedition.lcdui.*; implement Command
Listener
public class HelloMidlet extends MIDlet implements
CommandListener
{ Displayable.setCommadL
public void startApp() {} )istener(This
public void pauseApp() {}

public void destroyApp(boolean unconditional) {}


}{)public void commandAction(Command c, Displayable d
{
Method to service the
command
Enjoy with MIDlets
Let’s Go

Install these three


components in order

NetBeans Desktop Icon


Text Box

Commands
Forms
 A form includes collection of UI controls called Items
)public Form(String title
)public Form(String title,Item[] items
 public int append()
 public void set(int index,Item item)
 public void delete(int index)
 public void deleteAll()
 public int size()
 public Item get(int index)
Forms

 Form form = new Form(“Form


Title”);

 StringItem strItem = new


StringItem(“Label:”,“Value”);

 form.append(strItem);
Terminology

Soft Buttons

Navigation (Arrow) Buttons

Select (OK) Button

You might also like