J2ME_Introduction

Embed Size (px)

Citation preview

  • 8/7/2019 J2ME_Introduction

    1/44

    Introduction to J2ME

    ESPRIT Saif [email protected]

  • 8/7/2019 J2ME_Introduction

    2/44

    Java 2 Platform Micro Edition

    (J2ME)Java platform for small devices

    A subset of J2SE

    Released mid June 1999

    Target devices:

    -

    2Introduction to J2ME

    Mobile phones, smart phones

    PDAs (inc PocketPCs)

    TVs, VCRs, CD playersAlmost every mobile phone support J2ME

    [email protected]

  • 8/7/2019 J2ME_Introduction

    3/44

    J2ME Phones

    3Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    4/44

    J2ME Phones (Up to 15 Jan 2008)http://developers.sun.com/mobility/device/pub/device/list.do?sort=manufacturer&filterIds=61&page=1

    4Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    5/44

    3 Java Platforms

    Java 2 Platform

    5Introduction to J2ME

    Standard Edition

    (J2SE)

    Enterprise Edition

    (J2EE)

    Micro Edition

    (J2ME)

    Standard desktop &

    Workstation Applications

    Heavy duty server

    systems

    Small & memory

    Constrained devices

    [email protected]

  • 8/7/2019 J2ME_Introduction

    6/44

    6Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    7/44

    J2ME ArchitectureTo increase the flexibility of design, the J2MEconsists of two distinct layers:

    Configurations and Profiles

    Configuration

    7Introduction to J2ME

    range of devices with similar capabilities

    Profile

    Provides capabilities, on top of configuration, for aspecific device type

    [email protected]

  • 8/7/2019 J2ME_Introduction

    8/44

    J2ME Architecture

    Two types of J2MEconfigurations

    1. Connected DeviceConfiguration

    2. Connected Limited

    J2ME

    Profile

    Profile

    8Introduction to J2ME

    Device ConfigurationJ2ME

    Libraries

    JavaVirtual Machine

    Configuration

    CDC, orCLDC

    [email protected]

  • 8/7/2019 J2ME_Introduction

    9/44

    CLDC vs CDCCLDC

    160 Kbytes to 512 Kbytes of total memoryavailable16-bit or 32-bit processorLow power consumption and often operatingwith battery power

    9Introduction to J2ME

    Connectivity with limited bandwidth.CDC

    2Mbytes or more memory for Java platform

    32-bit processorHigh bandwidth network connection, mostoften using TCP/IP

    [email protected]

  • 8/7/2019 J2ME_Introduction

    10/44

    CLDC

    10Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    11/44

    Mobile Information Device Profile

    (MIDP)Is a set of APIs that allow developers to controlmobile device-specific problems

    i.e. user interfaces, local storage and clientapplication lifecycles etc.MIDlets minimum requirements

    11Introduction to J2ME

    two-way wireless network input device (i.e. keypad) 128 KB for CLDC/MIDP class and another 32 KB

    for the KVMMidlets are the most important and popularapplications in the J2ME family.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    12/44

    MIDP

    12Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    13/44

    Building J2ME Apps- ToolWe will use Sun Java Wireless Toolkit 2.x forCLDC (The newest version is 2.5.2 in Jan 2008)which can be downloaded fromhttp://java.sun.com/j2me/download.html

    13Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    14/44

    J2ME Wireless Toolkit DemoLaunch the Wireless Toolkit: Start > Programs > Sun Java(TM) Wireless Toolkit

    2.5.2 for CLDC

    14Introduction to J2ME

    WTK already includes a set of demo programs readyto run.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    15/44

    J2ME Wireless Toolkit DemoSelect menu item

    File > Open Project ...Select UIDemo andclick Open Project .

    15Introduction to J2ME

    The projects can be used as the templates of yourapplications.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    16/44

    J2ME Wireless Toolkit DemoClick the Build and then the Run buttons.

    16Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    17/44

    J2ME Wireless Toolkit DemoThe main menu screen is shown up. You can choosea program and select Launch to start the program.

    17Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    18/44

    MIDlet ProgrammingAny MIDP application must extends MIDlet

    This is the MIDP equivalent of an applet, wherestarting/stopping is under the control of theenvironment

    Like Java applets, MIDlets have an application lifec cle while runnin on a mobile device.

    18Introduction to J2ME

    [email protected]

  • 8/7/2019 J2ME_Introduction

    19/44

    MIDlet Transition StatesSpecifically, a MIDlet can be in one of three states asshown:

    19Introduction to J2ME

    Why do we needa Paused state?

    [email protected]

  • 8/7/2019 J2ME_Introduction

    20/44

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

    public class MyApp extends MIDlet {

    public void startApp() {// start up code

    }

    Note that startApp(), pauseApp()and destroyApp() are abstractmethods.

    You Midlet program mustoverride these 3 methods eventhough you are not do anythingin it.

    20Introduction to J2ME

    // we aren't showing any more

    }

    public void destroyApp(boolean unconditional) {// clean up

    }}

    [email protected]

  • 8/7/2019 J2ME_Introduction

    21/44

    Two Level APIThere are two areas the API which you should beconcerned with - 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

    21Introduction to J2ME

    keyed eventsAll MIDlet applications need to import the necessarymidlet and lcdui packages: import javax.microedition.midlet.*; import javax.microedition.lcdui.*;

    [email protected]

  • 8/7/2019 J2ME_Introduction

    22/44

    Displaying ObjectsHigh-level Screens have a base class calledDisplayable .

    To show something on a MIDP device, you need toobtain the devices display javax.microedition.lcdui.Display class.

    22Introduction to J2ME

    manager for each active MIDlet and providesinformation about the devices display capability.

    Subclassed Displayable classes will fill the whole

    screen

    [email protected]

  • 8/7/2019 J2ME_Introduction

    23/44

    Displaying ObjectsTo show a Displable object you must use thesetCurrent() method on the Display object.Form mainForm = new Form ("First Program ");Display display = Display.getDisplay(this);

    display .setCurrent (mainForm);

    23Introduction to J2ME

    Note that Form is a Displayable subclass.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    24/44

    First Example - HelloWorldimport javax.microedition.midlet.*;import javax.microedition.lcdui.*;

    public class HelloWorld extends MIDlet {

    public HelloWorld() {}

    public void startApp() {

    24Introduction to J2ME

    Form form = new Form( "First Program" );form.append( "Hello World" );Display.getDisplay(this).setCurrent ( form );

    }

    public void pauseApp() {}

    public void destroyApp( boolean unconditional ) {

    }}[email protected]

  • 8/7/2019 J2ME_Introduction

    25/44

    Building the MIDletRun the program KToolbarStart >Programs >J2ME Wireless Toolkit 2.x >KToolbar

    Click on New Project and enter the Project name andclass name as shown below

    25Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    26/44

    Building the MIDletAfter pressing the Create Project Button, a directorytree will be created for the project:

    26Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    27/44

    Building the MIDletUse TextPad to create a source file HelloWorld.javaand save it under the directory src .

    27Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    28/44

    Building and Run the MIDletClick the Build and then the Run buttons.

    28Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    29/44

    How can the program exit?The program can not exit unless you close theemulator.

    To provide a way to exit the program, you need touse Commands.A command is like a button, it has a title, like "OK" or"Cancel," and our a lication can res ond

    29Introduction to J2ME

    appropriately when the user invokes the command.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    30/44

    Event Handling with CommandsDisplayable, the parent of all screen displays,supports Commands .

    The device determines how the commands areshown on the screen or invoked by user.

    Every Displayable keeps a list of its Commands. You

    30Introduction to J2ME

    can a an remove omman s us ng e o ow ngmethods:

    public void addCommand(Command cmd)

    public void removeCommand(Command cmd)

    [email protected]

  • 8/7/2019 J2ME_Introduction

    31/44

    Command ObjectsIn J2ME, commands are commonly represented withsoft-buttons on the device. The following diagramshows two Command objects, one with the label"Exit" and one with label "View."

    31Introduction to J2ME

    soft-buttons

    [email protected]

  • 8/7/2019 J2ME_Introduction

    32/44

    Command ObjectsIf there are too many commands to be shown on thedisplay, a device will create a menu to hold multiplecommands. The following diagram shows how this

    might look.

    32Introduction to [email protected]

  • 8/7/2019 J2ME_Introduction

    33/44

    Use Command objectsThe basic steps to process events with a Commandobject are as follows:

    1. Create a Command object.

    2. Add the Command to a Form (or other GUIobjects TextBox, List, or Canvas).

    33Introduction to J2ME

    . .

    Upon detection of an event, the listener will call themethod commandAction().

    [email protected]

  • 8/7/2019 J2ME_Introduction

    34/44

    Create a CommandTo create a Command, you need to supply a label, atype, 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.

    34Introduction to J2ME

    CANCEL standard negative answer to a dialogEXIT for exiting from the application.

    HELP a request for on-line help.

    ITEM specific to the items of the Screen or the elements of aChoice.

    OK standard positive answer to a dialog

    SCREEN an application-defined command

    STOP A command that will stop some currently runningprocess, operation, etc.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    35/44

    Create a CommandTo create a standard OK command, for example, youwould do this:Command c = new Command("OK", Command.OK, 0);

    labeltype priority

    35Introduction to J2ME

    To create a command specific to your application,you might do this:Command c = new Command(

    "Launch", Command.SCREEN, 0);

    [email protected]

  • 8/7/2019 J2ME_Introduction

    36/44

    Priority and Long LabelEvery command has a priority.

    Lower numbers indicate a higher priority.

    If you add a command with priority 0, then severalmore with priority 1, the priority 0 command will showup on the screen directly. The other commands willmost likel end u in a secondar menu.

    36Introduction to J2ME

    MIDP also supports for long labels on commands.You can create a command with a short and longlabel like this:

    Command c = new Command("Run", "Run simulation" ,Command.SCREEN, 0);

    The device decides which label it will use based onthe available screen space and the size of the labels.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    37/44

    Responding to CommandsCommands show up on the screen, but nothinghappens automatically when a user invokes acommand.

    You need to write an object called a listener whichwill be called when the user invokes any command ina Displayable.

    37Introduction to J2ME

    The listener is an object that implements theCommandListener interface.To register the listener with a Displayable, use thefollowing method:

    public void setListener (CommandListener l)Note it is one Listener per Displayable, NOT oneListener per one Command.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    38/44

    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);

    38Introduction to J2ME

    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. Willbe called when any command in the Form isselected.

    [email protected]

  • 8/7/2019 J2ME_Introduction

    39/44

    39Introduction to [email protected]

    Another Command Example

  • 8/7/2019 J2ME_Introduction

    40/44

    Another Command Example

    (Two Forms)Launch

    Exit

    40Introduction to J2ME

    Exit

    2nd Form

    Go to First Form

    [email protected]

    Another Command Example (Two

  • 8/7/2019 J2ME_Introduction

    41/44

    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;

    41Introduction to J2ME

    // commandCommand 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);

    [email protected]

    Another Command Example (Two

  • 8/7/2019 J2ME_Introduction

    42/44

    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);

    42Introduction to J2ME

    f2 = new Form( "Form 2" );f2.append( "This is Form No. 2" );f2.addCommand(firstFormCommand);f2.addCommand(exitCommand);f2.setCommandListener(this);

    display.setCurrent( f1 );}

    [email protected]

    Another Command Example (Two

  • 8/7/2019 J2ME_Introduction

    43/44

    Another Command Example (Two

    Forms)public void pauseApp() {}

    public void destroyApp(boolean unconditional) {

    }

    public void commandAction(Command c, Displayable d) {String label = c.getLabel();

    43Introduction to J2ME

    a e .equa s " x t"notifyDestroyed();

    } else if (label.equals("1st Form")) {Display.getDisplay(this).setCurrent( f1 );

    } else {Display.getDisplay(this).setCurrent( f2 );

    }}

    }

  • 8/7/2019 J2ME_Introduction

    44/44

    Simple DebuggingSystem.out.print and System.out.println can be usedfor debugging.

    When run in the simulator, the output is put on theconsole, not the phone.

    public void commandAction(Command c, Displayable d) {

    44Introduction to J2ME

    .

    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 );

    }