introduction to j2me
DESCRIPTION
Introduction to J2ME. 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. J2ME Phones. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/1.jpg)
Introduction to J2ME
![Page 2: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/2.jpg)
2Introduction to J2ME
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
![Page 3: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/3.jpg)
3Introduction to J2ME
J2ME Phones
![Page 4: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/4.jpg)
4Introduction to J2ME
3 Java Platforms
Java2 Standard Edition
(J2SE)
Java2 Enterprise Edition
(J2EE)
Java2 Micro Edition
(J2ME)
Java 2 Platform
Standard desktop &Workstation Applications
Heavy duty serversystems
Small & memory Constrained devices
![Page 5: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/5.jpg)
5Introduction to J2ME
![Page 6: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/6.jpg)
6Introduction to J2ME
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
![Page 7: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/7.jpg)
7Introduction to J2ME
J2ME Architecture
Two types of J2ME configurations
1. Connected Device Configuration
2. Connected Limited Device Configuration
J2ME Profile
J2ME Libraries
JavaVirtual Machine
Profile
Configuration
CDC, orCLDC
![Page 8: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/8.jpg)
8Introduction to J2ME
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
![Page 9: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/9.jpg)
9Introduction to J2ME
CLDC
![Page 10: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/10.jpg)
10Introduction to J2ME
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.
![Page 11: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/11.jpg)
11Introduction to J2ME
MIDP
![Page 12: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/12.jpg)
12Introduction to J2ME
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
![Page 13: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/13.jpg)
13Introduction to J2ME
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.
![Page 14: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/14.jpg)
14Introduction to J2ME
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.
![Page 15: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/15.jpg)
15Introduction to J2ME
J2ME Wireless Toolkit Demo Click the Build and then the Run buttons.
![Page 16: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/16.jpg)
16Introduction to J2ME
J2ME Wireless Toolkit Demo The main menu screen is shown up. You can choose
a program and select Launch to start the program.
![Page 17: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/17.jpg)
17Introduction to J2ME
MIDlet Programming Any MIDP application must extend 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.
![Page 18: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/18.jpg)
18Introduction to J2ME
MIDlet Transition States Specifically, a MIDlet can be in one of three states as
shown:
Why do we need a Paused state?
![Page 19: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/19.jpg)
19Introduction to J2ME
Midlet Skeletonimport javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class MyApp extends MIDlet { public void startApp() { // start up code } public void pauseApp() { // we aren't showing any more }
public void destroyApp(boolean unconditional) { // clean up }}
Note that startApp(), pauseApp() and destroyApp() are abstract methods.
You Midlet program must override these 3 methods even though you are not doing anything in it.
![Page 20: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/20.jpg)
20Introduction to J2ME
Two Level API There are two levels of the API
– 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.*;
![Page 21: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/21.jpg)
21Introduction to J2ME
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
![Page 22: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/22.jpg)
22Introduction to J2ME
Displaying Objects To show a Displayable 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.
![Page 23: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/23.jpg)
23Introduction to J2ME
First Example - HelloWorldimport 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 ) { }}
![Page 24: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/24.jpg)
24Introduction to J2ME
![Page 25: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/25.jpg)
25Introduction to J2ME
Building the MIDlet After pressing the Create Project Button, a directory
tree will be created for the project:
![Page 26: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/26.jpg)
26Introduction to J2ME
Building the MIDlet Use TextPad to create a source file HelloWorld.java
and save it under the directory src.
![Page 27: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/27.jpg)
27Introduction to J2ME
Building and Run the MIDlet Click the Build and then the Run buttons.
![Page 28: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/28.jpg)
28Introduction to J2ME
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.
![Page 29: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/29.jpg)
29Introduction to J2ME
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)
![Page 30: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/30.jpg)
30Introduction to J2ME
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
![Page 31: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/31.jpg)
31Introduction to J2ME
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.
![Page 32: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/32.jpg)
32Introduction to J2ME
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().
![Page 33: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/33.jpg)
33Introduction to J2ME
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.
![Page 34: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/34.jpg)
34Introduction to J2ME
Create a Command To create a standard OK command, for example, you
would do this:
Command c = new Command("OK", Command.OK, 0);
To create a command specific to your application, you might do this:
Command c = new Command(
"Launch", Command.SCREEN, 0);
labeltype priority
![Page 35: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/35.jpg)
35Introduction to J2ME
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.
![Page 36: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/36.jpg)
36Introduction to J2ME
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.
![Page 37: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/37.jpg)
37Introduction to J2ME
Exampleimport 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.
![Page 38: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/38.jpg)
38Introduction to J2ME
![Page 39: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/39.jpg)
39Introduction to J2ME
Another Command Example (Two Forms)
Launch
Exit
Exit
2nd Form
Go to First Form
![Page 40: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/40.jpg)
40Introduction to J2ME
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);
![Page 41: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/41.jpg)
41Introduction to J2ME
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 ); }
![Page 42: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/42.jpg)
42Introduction to J2ME
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 ); } }}
![Page 43: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/43.jpg)
43Introduction to J2ME
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 ); }
![Page 44: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/44.jpg)
J2ME User Interface I
![Page 45: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/45.jpg)
45Introduction to J2ME
Major classes in the lcdui package
To be discussed in this lecture
![Page 46: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/46.jpg)
46Introduction to J2ME
TextBox The simplest type of screen is the TextBox. TextBox allows the user to enter a string. Text input is a difficult task on mobile phones. Many
devices only have a numeric keypad, so entering a single character is a matter of one, two, three or four button presses.
A good MIDlet requires minimal user input.
an email TextBox
![Page 47: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/47.jpg)
47Introduction to J2ME
TextBox A TextBox is created by specifying four parameters:
public TextBox(String title, String text, int maxSize, int constraints)
The title is used as the screen title The text and maxSize determine the initial text and maximum
size of the text box. The constraints are used to restrict the user's input.
– ANY : allows any type of input.
– NUMERIC : restricts the input to integers.
– DECIMAL : allows numbers with fractional parts.
– PHONENUMBER : requires a telephone number.
– EMAILADDR : input must be an e-mail address.
– URL : input must be a web address.
![Page 48: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/48.jpg)
48Introduction to J2ME
TextBox Constraints The devices don't allow invalid input; for example, a NUMERIC
TextBox doesn't allow you to enter alphabetic characters. Constraints may be combined with the flags listed below. Constraints limit the behavior of users, while flags define the
behavior of the TextBox. The available flags are:
PASSWORD : characters are not shown when entered; generally, they are represented by asterisks.
UNEDITABLE : indicates text that cannot be edited.
![Page 49: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/49.jpg)
49Introduction to J2ME
TextBox FlagsSENSITIVE : indicates that text should not be stored. Some input
schemes store input from the user for later use in autocompletion. This flag indicates that the text should not be saved or cached.
NON_PREDICTIVE : indicates that you are expecting the user to enter text that any text-predicting input scheme will probably not be able to guess. For example, if you're expecting the user to enter an order number like Z51002S, you would use this flag to tell the input scheme to not bother trying to predict the input.
INITIAL_CAPS_WORD : is used for input where each word should be capitalized.
INITIAL_CAPS_SENTENCE indicates input where the first character of each sentence should be capitalized.
NOT all of these settings may be functional in all devices.
![Page 50: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/50.jpg)
50Introduction to J2ME
TextBox Flags The flags may be combined with any of the other
constraints using the OR operator ( | ). For example, to create a TextBox that asks the user
to enter a number password, you would do something like this:
Displayable d = new TextBox( "PIN", "", 8, TextField.NUMERIC | TextField.PASSWORD);
![Page 51: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/51.jpg)
51Introduction to J2ME
Password Be careful in using PASSWORD. For every character you enter, the password field
shows an asterisk or some other symbol. On mobile phones and other small devices, security
is less of a concern because the screens are smaller and much more difficult to read than a typical desktop monitor.
![Page 52: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/52.jpg)
52Introduction to J2ME
Example: Accept a string from TextBox and echo it
One TextBoxTwo Commands - Exit and GreetOne CommandListener
![Page 53: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/53.jpg)
53Introduction to J2ME
Exampleimport javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class TextBoxTest extends MIDlet implements CommandListener {
private Display display; private TextBox tbClip; private Command cmExit; private Command cmGreet;
public TextBoxTest() { cmExit = new Command("Exit", Command.EXIT, 0); cmGreet = new Command("Greet", Command.SCREEN, 1);
tbClip = new TextBox("Textbox Test", "", 20, TextField.ANY); tbClip.addCommand(cmExit); tbClip.addCommand(cmGreet); tbClip.setCommandListener(this); }
![Page 54: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/54.jpg)
54Introduction to J2ME
Example public void startApp() { display = Display.getDisplay(this); display.setCurrent(tbClip); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable s) { if (c == cmExit) notifyDestroyed(); else if (c == cmGreet) System.out.println("Hello " + tbClip.getString()); }}
![Page 55: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/55.jpg)
55Introduction to J2ME
Alerts An Alert is essentially a simple dialog box. There are
two types of Alert:
– modal, which displays the dialog until acknowledged by the user, and
– timed, which is displayed for a specified number of seconds.
The constructors for an Alert are shown below:
Alert(String title)
Alert(String title, String alertText, Image alertImage, AlertType alertType)
![Page 56: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/56.jpg)
56Introduction to J2ME
Alerts The AlertType class provides five types: ALARM,
CONFIRMATION, ERROR, INFO, and WARNING.
The AlertType component uses sound, rather than an image, to notify the user of an event.
By default, timed Alerts are created using a default timeout value; you can find out the default value by calling getDefaultTimeout().
To set the timeout value to five seconds, you could do this:
alert.setTimeout(5000);
If you want a modal alert, use the special value FOREVER:
alert.setTimeout(Alert.FOREVER);
![Page 57: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/57.jpg)
57Introduction to J2ME
Example Five Alerts The following example, FiveAlerts, shows all types of alert. The display has six commands (5 Alerts + 1 Exit).
The default timeout value is 2000ms = 2 seconds. i.e. The Alert screen will dismiss after 2 seconds.
![Page 58: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/58.jpg)
58Introduction to J2ME
Example - Five Alerts The Error Alert will stay until the user dismiss it. The Info Alert will stay for on the screen for 4
seconds. After the alert dismisses, the display will return to the
previous screen public void setCurrent(Alert alert)
or go to next screen if the following setCurrent is used:
public void setCurrent(Alert alert, Displayable nextDisplayable)
![Page 59: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/59.jpg)
59Introduction to J2ME
Example - Five Alertsimport javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class FiveAlerts extends MIDlet implements CommandListener { private Display disp;
private Form f; private Alert alarm; private Alert confirm; private Alert error; private Alert info; private Alert warning; private Command alarmCommand, confCommand, errCommand, infoCommand, warnCommand, exitCommand; public FiveAlerts() { alarmCommand = new Command("Alarm", Command.SCREEN, 1); confCommand = new Command("Confirm", Command.SCREEN, 1); errCommand = new Command("Error", Command.SCREEN, 1); infoCommand = new Command("Info", Command.SCREEN, 1); warnCommand = new Command("Warning", Command.SCREEN, 1); exitCommand = new Command("Exit", Command.EXIT, 0);
![Page 60: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/60.jpg)
60Introduction to J2ME
f = new Form("Five Alerts"); f.addCommand(alarmCommand); f.addCommand(confCommand); f.addCommand(errCommand); f.addCommand(infoCommand); f.addCommand(warnCommand); f.addCommand(exitCommand); f.setCommandListener(this);
alarm = new Alert("Alarm", "Your payment is due today.", null, AlertType.ALARM); confirm = new Alert("Confirmation", "Do you want to proceed?", null, AlertType.CONFIRMATION); error = new Alert("Network error", "A network error occurred. Please try again.", null, AlertType.ERROR);
![Page 61: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/61.jpg)
61Introduction to J2ME
info = new Alert("About", "This program is used to demonstrate use of Alert." + " It will displayed for 4 seconds", null, AlertType.INFO); warning = new Alert("Warning", "Memory is low.", null, AlertType.WARNING);
System.out.println("DefultTimeout = " + alarm.getDefaultTimeout());
error.setTimeout(Alert.FOREVER); info.setTimeout(4000); // display for 4 seconds }
public void startApp() { disp = Display.getDisplay(this); disp.setCurrent(f); }
![Page 62: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/62.jpg)
62Introduction to J2ME
public void pauseApp() { }
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) { if (c == alarmCommand) disp.setCurrent(alarm); else if (c == confCommand) disp.setCurrent(confirm); else if (c == errCommand) disp.setCurrent(error, f); else if (c == infoCommand) disp.setCurrent(info, f); else if (c == warnCommand) disp.setCurrent(warning, f); else if (c == exitCommand) notifyDestroyed(); }}
![Page 63: Introduction to J2ME](https://reader033.vdocuments.site/reader033/viewer/2022061602/5681367d550346895d9e0b7c/html5/thumbnails/63.jpg)
63Introduction to J2ME
DISMISS_COMMAND - Done MIDP implementation automatically supply a DISMISS_COMMAND to
dismiss a modal alert. For example, the Sun's emulator provides a Done command mapped
to a soft button.
You can replace the default DISMISS_COMMAND by using the addCommand() method.
When the application first adds a command to an Alert, DISMISS_COMMAND is implicitly removed.