500 likes | 852 Views
J2ME. JAVA 2 Micro Edition. JAVA editions. J2Enterprise Edition. J2 Standard Edition. J2 Micro Edition. Architectures. Desktop. machines. Java 2 Enterprise Edition (J2EE). Smart- cards. Java 2 Standard Edition (J2SE). Personal Profile. Foundation Profile. MIDP.
E N D
J2ME JAVA 2 Micro Edition
JAVA editions J2Enterprise Edition J2 Standard Edition J2 Micro Edition
Architectures Desktop machines Java 2 Enterprise Edition (J2EE) Smart- cards Java 2 Standard Edition (J2SE) Personal Profile Foundation Profile MIDP Java Card APIs CDC CLDC Java Virtual Machine KVM
KVM • The device requires the Java virtual machine named KVM • minimal amount of Java application programming interface • (Java API) libraries included it. • “K” means kilo, indicating that the J2ME runnable • environment virtualmachine size is measured in kilobytes, • not in megabytes
J2ME Configurations: CDC and CLDC Sun has split the J2ME into configurations. Configurations define virtual machine features and Java language features API classes exist for each configuration environment
CDC - Connected Device Configuration • This covers devices with large interface capabilities • Such as TV set top boxes • A large API is likely to be needed
Connected Limited Device Configuration (CLDC) • For devices which have simple user interfaces • and low memory • Devices like cellular phones and PDAs • The CLDC requires 160 kB to 512 kB of total memory • Virtual machine – the interpreter that runs applications • plus java.io, java.lang, java.util
Outside CLDC Scope • Specified in Profiles to be implemented on top of the CLDC: • User interface support • Event handling • High-level application model • Persistence support remaining APIs: MIDP
Profiles • The Mobile Information device Profile (MIDP) is a further set of APIs for mobile devices • javax.microedition.midlet (MIDlet lifecycle), javax.microedition.lcdui (interface development), javax.microdedition.rms(storage facilities) You can find more information about MIDP and CLDC at the following URLs: http://java.sun.com/products/midp http://java.sun.com/products/cldc
MIDlets • A MIDlet is a Mobile Information Device application that runs on a MIDP device • The name is similar to servlet and applet and it shares similar characteristics • They have a lifecycle and occupy various states during program execution
MIDlets continued • Servlet container loads the servlet into memory • MIDlets are loaded in a similar fashion MIDP developers store several MIDlets in a jar file – called a MIDlet suite • The MIDP device contains a program called the application management software (AMS)
Application Management Software (AMS) • MIDlet suite is held on the server • The AMS downloads the MIDlet suite from the server Application descriptor file Server MIDlet suite .jad AMS
Lifecycle of a MIDlet • AMS calls MIDlets constructor • AMS calls startApp (now activated) • If AMS calls method pauseApp then MIDlet is paused • A call of destroyApp terminates the MIDlets execution
Midlet package MIDlets are packaged in a JAR file including • Class files of the MIDlet(s) • Resource files • Manifest with application properties • Application Descriptors (JAD files) accompany • MIDlet JARs and provide deployment • information (name, version, size, etc.)
Wireless Toolkit • The J2ME Wireless Toolkit supports the development of Java • applications that run on devices compliant with the Mobile Information • Device Profile (MIDP), such as cellular phones. • The J2ME Wireless Toolkit supports a number of ways to develop • MIDP Applications: • - running the tools from the command line yourself • - relying on development environments • The toolkit provides a basic UI called KToolBar.
Mobile information device characteristics(minimum): 96 pixel wide 54 pixel height display Touch screen, keypad or keyboard Wireless connection of limited bandwidth 128k non volatile memory to store MIDP components 8k for persistent data 32k of volatile memory for Java run time
MIDP summary Defines and controls applications, handles graphics and user events, storage,networking, scheduling JAVA application MIDP CLDC Uses KVM, data types, io, util
Base UI Class: Displayable • The screen in MIDP is of a fixed size represented • by the displayable object • Displayable can be considered to be similar to a WAP card • It has two abstract subclasses: • Screen • Canvas
Screens and events A MIDlet uses screens for output. Only one screen at a time is visible Predefined screen types: Alert List TextBox
A reference to the device's display can be obtained by providing a MIDlet reference to the static getDisplay( ) method: public static Display getDisplay(MIDlet c); This is typically done in the startApp( ) method of a MIDlet, as follows: public class MyMIDlet extends MIDlet { Display display = null; public MyMIDlet( ) { // constructor } public void startApp( ) { display = Display.getDisplay(this); } // other methods }
Alert A pre defined screen providing information but with An associated timeout. It can also be modal. The purpose is to provide a signal for exceptional circumstances and errors
Two constructors for the Alert class public Alert(String title); public Alert(String title, String alertText, Image alertImage, AlertType alertType); Alert alert = new Alert("title"); alert.setTimeout(4000); Alert timedAlert = new Alert("Confirmation", "Your message has been sent!", null, AlertType.CONFIRMATION); TimedAlert.setTimeout(5000);
List A list of choices. List implements the Choice interface Three types: - Multiple - Exclusive - Implicit (fires a Command)
Display display = Display.getDisplay(this); List menu = new List("Edit", Choice.EXCLUSIVE); menu.append("Save"); menu.append("Move to"); menu.append("delete"); display.setCurrent(menu); Set up a command handler to listen for selection then Execute the necessary function associated with that selection – see tutorial
TextBox TextBox mytext = new TextBox(“Introduction”, “Welcome”, 256, TextField.ANY); Different types of input are defined within the TextField class
Form class • User interface controls can be grouped together on a screen called a Form • The Form class is a subclass of screen • Form items can be : Choicegroup(list), Datefield, Gauge (bar graph), ImageItem, StringItem, TextField
ChoiceGroup A choice group is very similar to a List, but used for embedding in a FORM ChoiceGroup mycg = new ChoiceGroup(“Courses”, Choice.MULTIPLE); mycg.append(“Maths”,null); mycg.append(“Science”,null);
MIDP Class Hierarchy Displayable Interface Choice Screen Canvas Form Alert TextBox List
MIDP: Class Hierarchy Item Interface Choice ChoiceGroup Guage TextField StringItem ImageItem DateField
Events • Actions and commands • A Command has a label, type and priority • Examples of Command types are: BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN and STOP
Command Class • A Command represents an action a user can select. Keys displayed on a screen are called ‘soft’ keys. • Selecting a soft key causes a Command to be selected • Typically a MIDlet can be made to listen for a Command and then execute some code in response
Soft keys being set up // create soft button commands selectCommand = new Command( "Select", Command.OK, 0 ); nextCommand = new Command( "Next", Command.OK, 0 ); backCommand = new Command( "Back", Command.BACK, 1 );
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class HelloWorld extends MIDlet implements CommandListener { private final static String a1 = "HelloWorld"; private final static String a2 = "Bye"; private Command exitCommand; private Command backCommand; private Display display; private TextBox t; public HelloWorld() { display = Display.getDisplay(this); exitCommand = new Command("Exit", Command.EXIT, 1); backCommand = new Command("Back", Command.BACK, 1); }
public void startApp() { List l = new List("Choose", Choice.IMPLICIT); l.append(a1, null); l.append(a2, null); l.addCommand(exitCommand); l.setCommandListener(this); display.setCurrent(l); }
public void helloWorld() { t = new TextBox(a1, "Hello World", 256, TextField.ANY); t.addCommand(exitCommand); t.addCommand(backCommand); t.setCommandListener(this); display.setCurrent(t); }
public void commandAction(Command c, Displayable s) { if (c == List.SELECT_COMMAND) { List l = (List) s; String choice = l.getString(l.getSelectedIndex()); if (choice.equals(a1)) { helloWorld(); } else if (choice.equals(a2)) { notifyDestroyed(); } } int commandType = c.getCommandType(); if (commandType == Command.BACK) startApp(); else if (commandType == Command.EXIT) notifyDestroyed(); }
Exercise – use JBuilder to develop a MIDlet Carry out the instructions in tutorials
Low level API • The high-level API requires you to use task-oriented abstractions to define what the user interface does. • You have no real control over what gets drawn on the screen -- the implementation selects the best approach for the device
Canvas class • Unlike the high-level API, the low-level API gives you complete access to the screen and to input events • You can use both the high-level and low-level APIs in the same application, but not at the same time • To use the low-level API in a MIDlet, you must write a class that extends the Canvas class
Canvas Class • Low level events such as key presses are only available on Canvases • The low level API defines methods for drawing text and graphics on the display • Such capabilities are only available for canvases all other screen types have painting automated • Repaint occurs when the system calls the canvas’ paint method
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class MyCanvas extends Canvas { private MIDlet midlet; public MyCanvas( MIDlet midlet ) { this.midlet = midlet; } ………
protected void paint( Graphics g ) { g.setColor( 255, 255, 255 ); g.fillRect( 0, 0, getWidth(), getHeight() ); g.setColor( 0, 0, 0 ); g.drawString( “Low level API calls!", getWidth()/2, 0, g.TOP | g.HCENTER ); } }
Calling the Canvas • standard drawing primitives you would expect, such as drawArc, drawLine, drawRect, and drawString. Color is supported in the API using a 24-bit RGB model, although the device might support less color than the API. The MyCanvas example simply erases the screen by painting it white and then draws a string (in black) at the center top of the screen. • You activate a canvas by calling the setCurrent method on the MIDlet's Display object, usually in the startApp method of the application's MIDlet class:
import javax.microedition.midlet.*; public class MyMIDlet extends MIDlet { private Display display; private MyCanvas canvas; public MyMIDlet(){ display = Display.getDisplay( this ); canvas = new MyCanvas( this ); } protected void startApp() { display.setCurrent( canvas ); } protected void pauseApp(){ } protected void destroyApp( boolean unconditional ){ } public void exit(){ destroyApp( true ); notifyDestroyed(); } }
Input Events • Pressing the keypad (keyPressed, keyRepeated, and keyReleased) • Using the pointer (pointerPressed, pointerDragged and pointerReleased) if a pointer is available on the device • Showing the canvas (showNotify, hideNotify).
public class MyCanvas extends GameCanvas { private String message = "Press any key"; int fireKey; int leftKey……. public MyCanvas( MIDlet midlet ) { super( midlet ); } protected void paint( Graphics g ){ g.setColor( 255, 255, 255 ); g.fillRect( 0, 0, getWidth(), getHeight() ); g.setColor( 0, 0, 0 ); g.drawString( message, getWidth()/2, 0, g.TOP | g.HCENTER ); } protected void keyPressed( int keyCode ){ if( keyCode == fireKey ){ message = "FIRE"; } else if( keyCode == leftKey ){ message = "LEFT"; } else if( keyCode == rightKey ){ message = "RIGHT"; } else if( keyCode == upKey ){ message = "UP"; } else if( keyCode == downKey ){ message = "DOWN"; } else { message = getKeyName( keyCode ); } repaint(); }}
import javax.microedition.midlet.*; public class MyMIDlet extends MIDlet implements CommandListener { private Display display; private MyCanvas canvas; private Command exitCommand = new Command("Exit", Command.SCREEN, 1 ); public MyMIDlet(){ display = Display.getDisplay( this ); canvas = new MyCanvas( this ); canvas.addCommand( exitCommand ); canvas.setListener( this ); } protected void startApp(){ display.setCurrent( canvas ); } protected void pauseApp(){ } protected void destroyApp( boolean unconditional ){ } public void exit(){ destroyApp( true ); notifyDestroyed(); } public void commandAction( Command c, Displayable d ) { if( c == exitCommand ) { exit(); } }} The CommandListener interface defines a single method, commandAction, that is called whenever a command is triggered
Summary A MIDlet typically has a Constructor for initialisation A starttApp() method which is called whenever the MIDlet Becomes the device’s current process A pauseApp() A commandAction() A destroyApp() The Wireless toolkit enables us to build and run MIDlets It can be integrated into an IDE.
The MIDlet Suite MIDlets in a MIDlet suite can interact with each other MIDlets are stored in the JAR file Other files include: The MIDlet Application Descriptor file (JAD) The MIDlet Suite Manifest File (describes the contents of the MIDlet Suite)