Introduction to J 2 ME Java 2 Platform































































- Slides: 63
Introduction to J 2 ME
Java 2 Platform Micro Edition (J 2 ME) n Java platform for small devices n A subset of J 2 SE n Released mid June 1999 n Target devices: – Two-way pagers – Mobile phones, smart phones – PDAs (inc Pocket. PCs) – TVs, VCRs, CD players n Almost every mobile phone support J 2 ME Introduction to J 2 ME 2
J 2 ME Phones Introduction to J 2 ME 3
3 Java Platforms Java 2 Platform Java 2 Standard Edition (J 2 SE) Java 2 Enterprise Edition (J 2 EE) Java 2 Micro Edition (J 2 ME) Standard desktop & Workstation Applications Heavy duty server systems Small & memory Constrained devices Introduction to J 2 ME 4
Introduction to J 2 ME 5
J 2 ME Architecture n To increase the flexibility of design, the J 2 ME consists of two distinct layers: Configurations and Profiles n Configuration – Defines the minimum Java technology for a broad range of devices with similar capabilities n Profile – Provides capabilities, on top of configuration, for a specific device type Introduction to J 2 ME 6
J 2 ME Architecture n Two types of J 2 ME configurations J 2 ME Profile 1. Connected Device Configuration 2. Connected Limited Device Configuration J 2 ME Libraries Profile Configuration CDC, or CLDC Java Virtual Machine Introduction to J 2 ME 7
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. n CDC 2 Mbytes or more memory for Java platform 32 -bit processor High bandwidth network connection, most often using TCP/IP n Introduction to J 2 ME 8
CLDC Introduction to J 2 ME 9
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. n 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 n Midlets are the most important and popular applications in the J 2 ME family. n Introduction to J 2 ME 10
MIDP Introduction to J 2 ME 11
Building J 2 ME Apps- Tool n 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/j 2 me/download. html Introduction to J 2 ME 12
J 2 ME Wireless Toolkit Demo n Launch the Wireless Toolkit: – Start > Programs > Sun Java(TM) Wireless Toolkit 2. 5. 2 for CLDC n WTK already includes a set of demo programs ready to run. Introduction to J 2 ME 13
J 2 ME Wireless Toolkit Demo Select menu item File > Open Project. . . n Select UIDemo and click Open Project. n n The projects can be used as the templates of your applications. Introduction to J 2 ME 14
J 2 ME Wireless Toolkit Demo n Click the Build and then the Run buttons. Introduction to J 2 ME 15
J 2 ME Wireless Toolkit Demo n The main menu screen is shown up. You can choose a program and select Launch to start the program. Introduction to J 2 ME 16
MIDlet Programming n Any MIDP application must extend MIDlet n This is the MIDP equivalent of an applet, where starting/stopping is under the control of the environment n Like Java applets, MIDlets have an application life cycle while running on a mobile device. Introduction to J 2 ME 17
MIDlet Transition States n Specifically, a MIDlet can be in one of three states as shown: Why do we need a Paused state? Introduction to J 2 ME 18
Midlet Skeleton import javax. microedition. midlet. *; import javax. microedition. lcdui. *; public class My. App extends MIDlet { public void start. App() { // start up code } public void pause. App() { // we aren't showing any more } Note that start. App(), pause. App() and destroy. App() are abstract methods. You Midlet program must override these 3 methods even though you are not doing anything in it. public void destroy. App(boolean unconditional) { // clean up } } Introduction to J 2 ME 19
Two Level API n There are two levels of the API – the high and low-level API. n High-Level Provides input elements such as, – text fields, choices, and form Low-level is for drawing on Canvases and capturing keyed events n All MIDlet applications need to import the necessary midlet and lcdui packages: – import javax. microedition. midlet. *; – import javax. microedition. lcdui. *; n Introduction to J 2 ME 20
Displaying Objects n High-level Screens have a base class called Displayable. n To show something on a MIDP device, you need to obtain the device’s display – javax. microedition. lcdui. Display class. n This Display class is the one and only display manager for each active MIDlet and provides information about the device’s display capability. n Subclassed Displayable classes will fill the whole screen Introduction to J 2 ME 21
Displaying Objects n To show a Displayable object you must use the set. Current() method on the Display object. Form main. Form = new Form ("First Program "); Display display = Display. get. Display(this); display. set. Current (main. Form); Note that Form is a Displayable subclass. Introduction to J 2 ME 22
First Example - Hello. World import javax. microedition. midlet. *; import javax. microedition. lcdui. *; public class Hello. World extends MIDlet { public Hello. World() { } public void start. App() { } Form form = new Form( "First Program" ); form. append( "Hello World" ); Display. get. Display(this). set. Current( form ); public void pause. App() { } } public void destroy. App( boolean unconditional ) { } Introduction to J 2 ME 23
Introduction to J 2 ME 24
Building the MIDlet n After pressing the Create Project Button, a directory tree will be created for the project: Introduction to J 2 ME 25
Building the MIDlet n Use Text. Pad to create a source file Hello. World. java and save it under the directory src. Introduction to J 2 ME 26
Building and Run the MIDlet n Click the Build and then the Run buttons. Introduction to J 2 ME 27
How can the program exit? n The program can not exit unless you close the emulator. n To provide a way to exit the program, you need to use Commands. n 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. Introduction to J 2 ME 28
Event Handling with Commands n Displayable, the parent of all screen displays, supports Commands. n The device determines how the commands are shown on the screen or invoked by user. n Every Displayable keeps a list of its Commands. You can add and remove Commands using the following methods: – public void add. Command(Command cmd) – public void remove. Command(Command cmd) Introduction to J 2 ME 29
Command Objects n In J 2 ME, 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 Introduction to J 2 ME 30
Command Objects n 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. Introduction to J 2 ME 31
Use Command objects n 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 Text. Box, List, or Canvas). 3. Create and set a listener for the Form. n Upon detection of an event, the listener will call the method command. Action(). Introduction to J 2 ME 32
Create a Command n To create a Command, you need to supply a label, a type, and a priority. n 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 OK specific to the items of the Screen or the elements of a Choice. standard positive answer to a dialog SCREEN an application-defined command STOP A command that will stop some currently running process, operation, etc. Introduction to J 2 ME 33
Create a Command n To create a standard OK command, for example, you would do this: Command c = new Command("OK", Command. OK, 0); label type n priority To create a command specific to your application, you might do this: Command c = new Command( "Launch", Command. SCREEN, 0); Introduction to J 2 ME 34
Priority and Long Label n Every command has a priority. n Lower numbers indicate a higher priority. n 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. n MIDP also supports for long labels on commands. n You can create a command with a short and long label like this: Command c = new Command("Run", "Run simulation", Command. SCREEN, 0); n The device decides which label it will use based on the available screen space and the size of the labels. Introduction to J 2 ME 35
Responding to Commands n n n 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 Command. Listener interface. To register the listener with a Displayable, use the following method: – public void set. Listener(Command. Listener l) Note it is one Listener per Displayable, NOT one Listener per one Command. Introduction to J 2 ME 36
Example import javax. microedition. midlet. *; import javax. microedition. lcdui. *; public class Commander extends MIDlet implements Command. Listener { public void start. App() { Displayable d = new Form( "Test Command" ); Command c = new Command("Exit", Command. EXIT, 0); d. add. Command(c); d. set. Command. Listener(this); Display. get. Display(this). set. Current(d); } public void pause. App() { } public void destroy. App(boolean unconditional) { } } public void command. Action(Command c, Displayable s) { notify. Destroyed(); } Abstract method of Command. Listener. Will be called when any command in the Form is selected. Introduction to J 2 ME 37
Introduction to J 2 ME 38
Another Command Example (Two Forms) Launch Exit 2 nd Form Exit Go to First Form Introduction to J 2 ME 39
Another Command Example (Two Forms) import javax. microedition. lcdui. *; import javax. microedition. midlet. *; public class Commander 2 extends MIDlet implements Command. Listener { Display display = null; Form f 1 = null; Form f 2 = null; // command Command first. Form. Command = new Command("1 st Form", "Go to First Form", Command. SCREEN, 0); Command second. Form. Command = new Command("2 nd Form", "Go to Second Form", Command. SCREEN, 0); Command exit. Command = new Command("Exit", Introduction Command. EXIT, 1); to J 2 ME 40
Another Command Example (Two Forms) public void start. App() { display = Display. get. Display(this); f 1 = new Form( "Form 1" ); f 1. append( "This is Form No. 1" ); f 1. add. Command(second. Form. Command); f 1. add. Command(exit. Command); f 1. set. Command. Listener(this); f 2 = new Form( "Form 2" ); f 2. append( "This is Form No. 2" ); f 2. add. Command(first. Form. Command); f 2. add. Command(exit. Command); f 2. set. Command. Listener(this); } display. set. Current( f 1 ); Introduction to J 2 ME 41
Another Command Example (Two Forms) public void pause. App() { } public void destroy. App(boolean unconditional) { } } public void command. Action(Command c, Displayable d) { String label = c. get. Label(); if (label. equals("Exit")) { notify. Destroyed(); } else if (label. equals("1 st Form")) { Display. get. Display(this). set. Current( f 1 ); } else { Display. get. Display(this). set. Current( f 2 ); } } Introduction to J 2 ME 42
Simple Debugging n System. out. print and System. out. println can be used for debugging. n When run in the simulator, the output is put on the console, not the phone. public void command. Action(Command c, Displayable d) { String label = c. get. Label(); if (label. equals("Exit")) { notify. Destroyed(); } else if (label. equals("1 st Form")) { System. out. println("1 st Form is called"); display. set. Current( f 1 ); } else { System. out. println("2 nd Form is called"); display. set. Current( f 2 ); } Introduction to J 2 ME 43
J 2 ME User Interface I
Major classes in the lcdui package To be discussed in this lecture Introduction to J 2 ME 45
Text. Box n The simplest type of screen is the Text. Box. n Text. Box allows the user to enter a string. n 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. n A good MIDlet requires minimal user input. an email Text. Box Introduction to J 2 ME 46
Text. Box n A Text. Box is created by specifying four parameters: public Text. Box(String title, String text, int max. Size, int constraints) n The title is used as the screen title n The text and max. Size determine the initial text and maximum size of the text box. n 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. Introduction to J 2 ME 47
Text. Box Constraints n The devices don't allow invalid input; for example, a NUMERIC Text. Box doesn't allow you to enter alphabetic characters. n Constraints may be combined with the flags listed below. n Constraints limit the behavior of users, while flags define the behavior of the Text. Box. n The available flags are: PASSWORD : characters are not shown when entered; generally, they are represented by asterisks. UNEDITABLE : indicates text that cannot be edited. Introduction to J 2 ME 48
Text. Box Flags SENSITIVE : 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 Z 51002 S, 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. n NOT all of these settings may be functional in all devices. Introduction to J 2 ME 49
Text. Box Flags n The flags may be combined with any of the other constraints using the OR operator ( | ). n For example, to create a Text. Box that asks the user to enter a number password, you would do something like this: n Displayable d = new Text. Box( "PIN", "", 8, Text. Field. NUMERIC | Text. Field. PASSWORD); Introduction to J 2 ME 50
Password n Be careful in using PASSWORD. n For every character you enter, the password field shows an asterisk or some other symbol. n 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. Introduction to J 2 ME 51
Example: Accept a string from Text. Box and echo it One Text. Box Two Commands - Exit and Greet One Command. Listener Introduction to J 2 ME 52
Example import javax. microedition. midlet. *; import javax. microedition. lcdui. *; public class Text. Box. Test extends MIDlet implements Command. Listener { private Display display; private Text. Box tb. Clip; private Command cm. Exit; private Command cm. Greet; public Text. Box. Test() { cm. Exit = new Command("Exit", Command. EXIT, 0); cm. Greet = new Command("Greet", Command. SCREEN, 1); } tb. Clip = new Text. Box("Textbox Test", "", 20, Text. Field. ANY); tb. Clip. add. Command(cm. Exit); tb. Clip. add. Command(cm. Greet); tb. Clip. set. Command. Listener(this); Introduction to J 2 ME 53
Example public void start. App() { display = Display. get. Display(this); display. set. Current(tb. Clip); } public void pause. App() { } public void destroy. App(boolean unconditional) { } } public void command. Action(Command c, Displayable s) { if (c == cm. Exit) notify. Destroyed(); else if (c == cm. Greet) System. out. println("Hello " + tb. Clip. get. String()); } Introduction to J 2 ME 54
Alerts n 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. n The constructors for an Alert are shown below: Alert(String title) Alert(String title, String alert. Text, Image alert. Image, Alert. Type alert. Type) Introduction to J 2 ME 55
Alerts n The Alert. Type class provides five types: ALARM, CONFIRMATION, ERROR, INFO, and WARNING. n The Alert. Type component uses sound, rather than an image, to notify the user of an event. n By default, timed Alerts are created using a default timeout value; you can find out the default value by calling get. Default. Timeout(). n To set the timeout value to five seconds, you could do this: alert. set. Timeout(5000); n If you want a modal alert, use the special value FOREVER: alert. set. Timeout(Alert. FOREVER); Introduction to J 2 ME 56
Example Five Alerts n The following example, Five. Alerts, shows all types of alert. n The display has six commands (5 Alerts + 1 Exit). n The default timeout value is 2000 ms = 2 seconds. i. e. The Alert screen will dismiss after 2 seconds. Introduction to J 2 ME 57
Example - Five Alerts n The Error Alert will stay until the user dismiss it. n The Info Alert will stay for on the screen for 4 seconds. n After the alert dismisses, the display will return to the previous screen public void set. Current(Alert alert) or go to next screen if the following set. Current is used: public void set. Current(Alert alert, Displayable next. Displayable) Introduction to J 2 ME 58
Example - Five Alerts import javax. microedition. midlet. *; import javax. microedition. lcdui. *; public class Five. Alerts extends MIDlet implements Command. Listener { private Display disp; private Form f; private Alert alarm; private Alert confirm; private Alert error; private Alert info; private Alert warning; private Command alarm. Command, conf. Command, err. Command, info. Command, warn. Command, exit. Command; public Five. Alerts() { alarm. Command = new Command("Alarm", Command. SCREEN, 1); conf. Command = new Command("Confirm", Command. SCREEN, 1); err. Command = new Command("Error", Command. SCREEN, 1); info. Command = new Command("Info", Command. SCREEN, 1); warn. Command = new Command("Warning", Command. SCREEN, 1); exit. Command = new Command("Exit", Command. EXIT, 0); Introduction to J 2 ME 59
f = new Form("Five Alerts"); f. add. Command(alarm. Command); f. add. Command(conf. Command); f. add. Command(err. Command); f. add. Command(info. Command); f. add. Command(warn. Command); f. add. Command(exit. Command); f. set. Command. Listener(this); alarm = new Alert("Alarm", "Your payment is due today. ", null, Alert. Type. ALARM); confirm = new Alert("Confirmation", "Do you want to proceed? ", null, Alert. Type. CONFIRMATION); error = new Alert("Network error", "A network error occurred. Please try again. ", null, Alert. Type. ERROR); Introduction to J 2 ME 60
info = new Alert("About", "This program is used to demonstrate use of Alert. " + " It will displayed for 4 seconds", null, Alert. Type. INFO); warning = new Alert("Warning", "Memory is low. ", null, Alert. Type. WARNING); System. out. println("Defult. Timeout = " + alarm. get. Default. Timeout()); error. set. Timeout(Alert. FOREVER); info. set. Timeout(4000); // display for 4 seconds } public void start. App() { disp = Display. get. Display(this); disp. set. Current(f); } Introduction to J 2 ME 61
public void pause. App() { } public void destroy. App(boolean unconditional) {} public void command. Action(Command c, Displayable s) { if (c == alarm. Command) disp. set. Current(alarm); else if (c == conf. Command) disp. set. Current(confirm); else if (c == err. Command) disp. set. Current(error, f); else if (c == info. Command) disp. set. Current(info, f); else if (c == warn. Command) disp. set. Current(warning, f); else if (c == exit. Command) notify. Destroyed(); } } Introduction to J 2 ME 62
DISMISS_COMMAND - Done MIDP implementation automatically supply a DISMISS_COMMAND to dismiss a modal alert. n For example, the Sun's emulator provides a Done command mapped to a soft button. n You can replace the default DISMISS_COMMAND by using the add. Command() method. n When the application first adds a command to an Alert, DISMISS_COMMAND is implicitly removed. n Introduction to J 2 ME 63