You are on page 1of 10

1

Introduction to programming in J2ME


CE00213-M Mobile Applications and Systems
Introduction to programming in J2ME
2
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
Basics of J2ME
Objectives
Understand the AMS states and lifecycle
Understand a simple Hello World program
Appreciate different command objects
Appreciate different command types
Appreciate J2ME user-interface API class hierarchy



2 2
3
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
Mobile Information Device Profile (MIDP)
MIDP device contains a program called the application
management software (AMS) which downloads the MIDlet suite
from the server, opens the MIDlet suite, then launches the user-
specified MIDlet on the MIDP device
LifeCycle of a midlet
paused state
constructor method
called
active state
startApp method
called
pauseApp method
called
destroyApp method
called
4
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
A simple program
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet
{
private Form form;
private Display display;

public HelloWorld()
{
super();
}
public void startApp()
{
form = new Form("A simple MIDlet");
String msg = "Hello, World";
form.append(msg);
display = Display.getDisplay(this);
display.setCurrent(form);
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
notifyDestroyed();
form = null;
System.out.println("application destroyed");
}
}
5
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
The same program
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet
{
private Form form;
private Display display;

public HelloWorld()
{
form = new Form("A simple MIDlet");
String msg = "Hello, World";
form.append(msg);
display = Display.getDisplay(this);


}
public void startApp()
{
display.setCurrent(form);
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
notifyDestroyed();
form = null;
System.out.println("application destroyed");
}
}
6
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
Command Objects
Soft-Buttons and keys
Create a command object
private Command cmHelp
cmHelp = new Command("Help", Command.HELP, 1);

Add the command object to a form
fmMain.addCommand(cmHelp);

Add a listener to the form
fmMain.setCommandListener(this);

Perform an action
public void commandAction(Command c, Displayable s)
{
if (c == cmHelp)
display.setCurrent(tbHelp);
}




7
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
Command types
BACK
CANCEL
EXIT
HELP
ITEM
A request to map the Command to an item on the screen. E.g. when using a List component,
you can mimic the functionality of a context-sensitive menu by mapping Commands to the
various entries In the list
OK
SCREEN
For commands in which it is unlikely there will be a specific key mapping available. For
example you might have Commands to initiate uploading and downloading of data. The
labels Upload and Download will not have direct key mappings on the a device
STOP
8
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CommandExample extends MIDlet implements CommandListener
{
private Display display; // Reference to Display object for this MIDlet
private Form form; // Main Form
private TextBox texthelp; // Textbox to display help message
private Command exit; // Exit the MIDlet
private Command help; // Ask for Help
private Command back; // Go "back" to the main form

public CommandExample()
{
display = Display.getDisplay(this);

help = new Command("Help", Command.HELP, 1);
back = new Command("Back", Command.BACK, 1);
exit = new Command("Exit", Command.EXIT, 1);

// Create the Form, add Commands, listen for events
form = new Form("Command Example");
form.addCommand(exit);
form.addCommand(help);
form.setCommandListener(this);

// Create the help Textbox with a maximum of 25 characters
texthelp = new TextBox("Help", "Help text here...", 25, 0);
texthelp.addCommand(back);
texthelp.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp(){ }
public void destroyApp(boolean unconditional){ }

public void commandAction(Command c, Displayable s)
{
if (c == exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == help)
display.setCurrent(texthelp);
else if (c == back)
display.setCurrent(form);
}
}
9
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ManyCommands extends MIDlet implements CommandListener
{
private Display display; // Reference to Display object for this MIDlet
private Form form; // The main Form
private TextBox feedback; // Textbox to show different messages
private Command exit; // Exit the MIDlet
private Command back; // Go "back" to the main form
private Command displayMessage1; // message1
private Command displayMessage2; // message2

public ManyCommands()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
back = new Command("Back", Command.BACK, 1);
displayMessage1 = new Command("Message1", Command.SCREEN, 2);
displayMessage2 = new Command("Message2", Command.SCREEN, 3);
// Create the Form, add Commands, listen for events
form = new Form("Many Commands");
form.addCommand(exit);
form.addCommand(displayMessage1);
form.addCommand(displayMessage2);
form.setCommandListener(this);
// Create a Textbox, add Command, listen for events
feedback = new TextBox("Message text", "Display Message data", 25, 0);
feedback.addCommand(back);
feedback.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp(){ }
public void destroyApp(boolean unconditional){ }
public void commandAction(Command c, Displayable s)
{
if (c == exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == displayMessage1 || c == displayMessage2)
display.setCurrent(feedback);
else if (c == back)
display.setCurrent(form);
}
}
10
Introduction to programming in J2ME
CE00213-M Mobile Applications and Systems
J2ME user-interface API class hierarchy
javax.microedition.lcdui.Displayable
Screen
Alert Form TextBox List
Canvas
(High Level) (Low Level)
Item
ChoiceGroup DateField ImageItem StringItem
TextField Guage

You might also like