Programming of Handheld and Mobile Devices

Download Report

Transcript Programming of Handheld and Mobile Devices

Programming of Handheld and Mobile
Devices
Lecture 14 Various MIDlet examples
Rob Pooley [email protected]
Programming Handheld and
Mobile devices
1
Example of TextBox 1
/*
* Based on HelloMIDlet.java from Sun Microsystems
* Original Author: Srikanth Raju
* Changed beyond recognition by Rob Pooley
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple TextBoxes
* These allow editing of an initial text
* which is then displayed in a second TextBox.
*/
public class HelloMIDlet extends MIDlet implements CommandListener
{
private Display display; // The display for this MIDlet
private TextBox t1, t2;
Command cExit, cDone, cRestart;
public HelloMIDlet() {
display = Display.getDisplay(this);
}
Programming Handheld and
Mobile devices
2
Example of TextBox 2
/**
* Start up the Hello MIDlet by creating the first TextBox
*/
public void startApp() {
t1 = new TextBox("Hello Heriot Watt", "We like Saturday!", 256, 0);
cExit = new Command("Exit",Command.EXIT,0);
t1.addCommand(cExit);
cDone = new Command("Done",Command.OK,0);
t1.addCommand(cDone);
t1.setCommandListener(this);
display.setCurrent(t1);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
Programming Handheld and
Mobile devices
3
Example of TextBox 3
public void commandAction(Command c, Displayable d)
{
switch(c.getCommandType())
{
case Command.EXIT: notifyDestroyed();
break;
case Command.OK: t2 = new TextBox(t1.getString(), "Oh yes we do!", 256, 0);
cRestart = new Command("Return",Command.BACK,0);
t2.addCommand(cRestart);
t2.setCommandListener(this);
display.setCurrent(t2);
break;
case Command.BACK: startApp();
break;
default:
}
}
}
break;
Programming Handheld and
Mobile devices
4
Example of Alert 1
/*
*
* Changed beyond recognition by Rob Pooley
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple Alerts
* These allow display of an initial text and simple drawing
*/
public class AlertExample extends MIDlet implements CommandListener
{
private Display display; // The display for this MIDlet
private Alert a1, a2;
Command cExit, cDone, cRestart;
Image i1,i2;
Programming Handheld and
Mobile devices
5
Example of Alert 2
public AlertExample() {
display = Display.getDisplay(this);
}
/**
* Start up the Hello MIDlet by creating the first Alert
*/
public void startApp() {
i1 = Image.createImage(30,30);
Graphics g = i1.getGraphics();
g.drawLine(0,0,10,10);
g.drawArc(10,10,10,10,0,360);
a1 = new Alert("Hello Heriot Watt", "We like Saturday!", i1, AlertType.INFO);
cExit = new Command("Exit",Command.EXIT,0);
a1.addCommand(cExit);
cDone = new Command("Done",Command.OK,0);
a1.addCommand(cDone);
a1.setCommandListener(this);
display.setCurrent(a1);
}
Programming Handheld and
Mobile devices
6
Example of Alert 3
/**
* Pause is a no-op since there are no background activities
*/
public void pauseApp() { }
/**
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d)
{
switch(c.getCommandType())
{
case Command.EXIT: notifyDestroyed();
break;
case Command.OK: a2 = new Alert("Oh yes we do!");
cRestart = new Command("Return",Command.BACK,0);
a2.addCommand(cRestart);
a2.setCommandListener(this);
display.setCurrent(a2);
break;
case Command.BACK: startApp();
break;
default:
break;
}
}
}
Programming Handheld and
Mobile devices
7
Example of List 1
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* An example MIDlet with simple Lists
* These allow display of a selectable menu of strings
*/
public class ListExamp extends MIDlet implements CommandListener
{
private Display display; // The display for this MIDlet
private List l1, l2;
Command cExit, cDone, cRestart, cSend, cLeave;
Image i1,i2;
public ListExamp() {
display = Display.getDisplay(this);
}
Programming Handheld and
Mobile devices
8
Example of List 2
/**
* Start up the Hello MIDlet by creating the first TextBox
*/
public void startApp() {
l1 = new List("Hello Heriot Watt",List.IMPLICIT);
cExit = new Command("Exit",Command.EXIT,0);
l1.addCommand(cExit);
cDone = new Command("Done",Command.OK,0);
l1.addCommand(cDone);
cSend = new Command("Send",Command.SCREEN,0);
l1.addCommand(cSend);
cLeave = new Command("Leave",Command.SCREEN,0);
l1.addCommand(cLeave);
l1.setCommandListener(this);
display.setCurrent(l1);
}
Programming Handheld and
Mobile devices
9
Example of List 3
/**
* Pause is a no-op since there are no background
activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must cleanup everything not handled by the
garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d)
{
switch(c.getCommandType())
{
case Command.EXIT: notifyDestroyed();
break;
case Command.OK: l2 = new List("Oh yes we
do!",List.EXCLUSIVE);
cRestart = new
Command("Return",Command.BACK,0);
l2.addCommand(cRestart);
l2.setCommandListener(this);
display.setCurrent(l2);
break;
case Command.BACK: startApp();
break;
case Command.SCREEN: if(c.getLabel()==“Leave")
notifyDestroyed();
else
{
Alert a1 = new Alert("Leave","Now
please",null,AlertType.ERROR);
display.setCurrent(a1);
}
break;
default:
break;
}
}
}
Programming Handheld and
Mobile devices
10
Example of Complex Form 1
public class CompForm extends MIDlet implements
CommandListener
{
private Display display; // The display for this
MIDlet
private Form f1, f2;
Command cExit, cDone, cRestart;
Image i1,i2;
ImageItem ii1, ii2;
TextField b1, b2;
public CompForm() {
display = Display.getDisplay(this);
}
/**
* Start up the Hello MIDlet by creating the Form
*/
public void startApp() {
Graphics g = i1.getGraphics();
g.drawLine(0,0,10,10);
g.drawArc(10,10,10,10,0,360);
i2 = Image.createImage(i1);
ii1 = new
ImageItem("Picture",i2,ImageItem.LAYOUT_CENT
ER,"Lopsided lolly");
b1 = new TextField("text
sample","1234567890",256,TextField.NUMERIC);
f1 = new Form("Hello Heriot Watt");
cExit = new Command("Exit",Command.EXIT,0);
f1.addCommand(cExit);
cDone = new Command("Done",Command.OK,0);
f1.addCommand(cDone);
f1.setCommandListener(this);
f1.append(ii1);
f1.append(b1);
i1 = Image.createImage(30,30);
display.setCurrent(f1);
}
Programming Handheld and
Mobile devices
11
Example of Complex Form 2
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}
/**
* Destroy must clean up everything not handled by the garbage collector.
* In this case there is nothing to clean up.
*/
public void destroyApp(boolean unconditional) {
}
Programming Handheld and
Mobile devices
12
Example of Complex Form 3
public void commandAction(Command c, Displayable d)
{
switch(c.getCommandType())
{
case Command.EXIT: notifyDestroyed();
break;
case Command.OK: f2 = new Form("Oh yes we do!");
cRestart = new Command("Return",Command.BACK,0);
f2.addCommand(cRestart);
f2.setCommandListener(this);
display.setCurrent(f2);
break;
case Command.BACK: startApp();
break;
default:
}
}
}
break;
Programming Handheld and
Mobile devices
13
Complex form in
use
Programming Handheld and
Mobile devices
14
Moving MIDlets
• There are two parts to installing MIDP for Palm OS.
• You must install the MIDP for Palm OS package on your
desktop computer, and
• you must install the MIDP for Palm OS implementation
(Java™ HQ) on your Palm OS device.
Programming Handheld and
Mobile devices
15
Result of the Installation
• Installing the MIDP for Palm OS implementation adds
the following icon to your device’s home screen:
Programming Handheld and
Mobile devices
16
Example of TextBox 3
Programming Handheld and
Mobile devices
17
Running applications written in Java
• This screen contains a copyright notice and a
Preferences button.
• Tapping the Preferences button gives you the option to
change the MIDP for Palm OS global preferences.
• This screen also provides help (a “tip”) if you tap the
icon.
• Java™ HQ cannot launch Java applications. You run a
Java application by tapping the application’s icon.
Programming Handheld and
Mobile devices
18
Installing Java Applications
• A Java application written for MIDP is distributed as a
pair of files,
– one with a .jar extension and
– the other with a .jad extension.
• Other than their extensions, the files have the same
name.
– For example, a poker Java application would be
distributed as the files Poker.jar and Poker.jad.
Programming Handheld and
Mobile devices
19
Converting
• You must convert it to a Palm OS application before you
can install it.
• A PalmOS application is a file that has a .prc extension.
• You use the Convertor tool to do this.
Programming Handheld and
Mobile devices
20
Packaging a Java application
•
•
•
•
The KToolBar will package your Java MIDlet
Select the Project menu
Then select Package from that menu
Then select Create Package from that menu
• The Package is created as a .jar and .jad pair in the bin
folder of the project
Programming Handheld and
Mobile devices
21
Convertor
• If your computer is configured
to run JAR files directly, you
could double-click on the
Converter.jar file.
Programming Handheld and
Mobile devices
22
To Change JAR/JAD Files Into a PRC File
• 1. Make sure the JAR and
JAD file are in the same
directory.
• 2. If the PRC Converter Tool
is not running, start it.
• A window similar to the one in
the following figure will appear:
Programming Handheld and
Mobile devices
23
Using the tool
• 3. Click the PRC Converter
Tool’s Folder button ( ), or
choose Convert from its File
menu.
• A dialog box similar to this
• 4. Navigate to the directory
where the JAD and JAR files
are located.
Programming Handheld and
Mobile devices
24
• 5. Select a JAD file to convert.
• The JAD info panel shows you information
about the Java application.
• Note that if your desktop computer system
supports selecting multiple files in dialog
boxes, you can select multiple JAD files to
convert.
• 6. Click the Convert button to convert the
JAR/JAD file pair to a PRC.
• (Clicking Convert does not affect the original
JAR/JAD files; it merely creates a new file with
a .prc extension.)
Programming Handheld and
Mobile devices
25
Multiple MIDP for Palm OS Icons on Palm OS
Device
Programming Handheld and
Mobile devices
26