Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley

Similar presentations


Presentation on theme: "Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley"— Presentation transcript:

1 Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley rjp@macs.hw.ac.uk

2 Programming Handheld and Mobile devices 2 J2ME Toolkit The J2ME toolkit provides –An IDE (but minus an editor) –A set of simulators/emulators –A set of examples You should find it on the menu –Start/Programs/J2ME Wireless Toolkit 2.2/KToolbar It supports general J2ME programming using the KVM

3 Programming Handheld and Mobile devices 3 Open the KToolBar

4 Programming Handheld and Mobile devices 4 Projects in KToolbar You can either open an existing project

5 Programming Handheld and Mobile devices 5 Pick a project

6 Programming Handheld and Mobile devices 6 Ready to build

7 Programming Handheld and Mobile devices 7 Or create one from scratch First create a Java source file using a text editor such as Notepad or any other of your choice –Don’t create Word or other specialised formats –Just plain text –Save as filename.java –Save into the appropriate directory Now select new project –Give the project name and the name of the MIDlet Now proceed as with opening an existing project

8 Programming Handheld and Mobile devices 8 Building and running Select build –If all is well you get confirmation that it worked –If there is a problem you need to edit the source to fix it, saving the source file –Then build again Now select run The selected device is used to show the project running Interact as if it was a real mobile phone or PDA

9 Programming Handheld and Mobile devices 9 import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class Bounder extends MIDlet implements CommandListener { public void startApp() { Display display = Display.getDisplay(this); Form mainForm = new Form("TinyMIDlet"); mainForm.append("Welcome to the world of MIDlets!"); Command exitCommand = new Command("Exit", Command.EXIT, 0); mainForm.addCommand(exitCommand); mainForm.setCommandListener(this); display.setCurrent(mainForm); } public void pauseApp () {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) notifyDestroyed(); } This small example shows the Lowest Common Denominator GUI API in use The MIDlet is also a CommandListener In startAPP –A Display object is set up and associated with this MIDlet. –A Form is set up with label TinyMIDlet. –The Form is also given the text “Welcome…” –A Command is set up with the label “Exit”, the type EXIT and priority 0 –This command is added to the Form, so that it will be triggerable when the form is in use. –This MIDlet is made a CommandListener for the Form –The Form is made the current object being displayed. commandAction is the CommandListener method which handles actions for the Form. –When an EXIT action occurs the MIDlet destroys itself

10 Programming Handheld and Mobile devices 10 Example import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class Bounder extends MIDlet implements CommandListener { public void startApp() { Display display = Display.getDisplay(this); Form mainForm = new Form("TinyMIDlet"); mainForm.append("Welcome to the world of MIDlets!"); Command exitCommand = new Command("Exit", Command.EXIT, 0); mainForm.addCommand(exitCommand); mainForm.setCommandListener(this); display.setCurrent(mainForm); } public void pauseApp () {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) notifyDestroyed(); } I put this in the folder C:\WTK22\apps\FirstTry\src I called it Bounder.java I created a project called FirstTry I specified the MIDlet as Bounder Build and run did the rest

11 Programming Handheld and Mobile devices 11 The quiz example phase 1 import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class GuessALot extends MIDlet implements CommandListener { public void startApp() { Display display = Display.getDisplay(this); Form mainForm = new Form("Quiz Game"); mainForm.append("Welcome to Guess a Lot"); Command exitCommand = new Command("Exit", Command.EXIT, 0); Command historyCommand = new Command("History", Command.OK, 0); Command mathsCommand = new Command("Mathematics", Command.ITEM, 0); mainForm.addCommand(exitCommand); mainForm.addCommand(historyCommand); mainForm.addCommand(mathsCommand); mainForm.setCommandListener(this); display.setCurrent(mainForm); }

12 Programming Handheld and Mobile devices 12 public void pauseApp () {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getCommandType()==Command.EXIT) notifyDestroyed(); else if (c.getCommandType()==Command.ITEM) ;//code to open Maths window else if (c.getCommandType()==Command.OK) ;//code to open history window else; //default case }

13 Programming Handheld and Mobile devices 13

14 Programming Handheld and Mobile devices 14 Running the MIDlet Press the launch button

15 Programming Handheld and Mobile devices 15 EXIT gets a default position Two more buttons so a menu is created

16 Programming Handheld and Mobile devices 16 Arrow keys to choose Select button to make selection

17 Programming Handheld and Mobile devices 17 Developing the quiz application public class GuessALot extends MIDlet implements CommandListener { Form mainForm, historyForm, mathsForm; Display display; public void startApp() { display = Display.getDisplay(this); mainForm = new Form("Quiz Game"); mainForm.append("Welcome to Guess a Lot"); Command exitCommand = new Command("Exit", Command.EXIT, 0); Command historyCommand = new Command("History", Command.OK, 0); Command mathsCommand = new Command("Mathematics", Command.ITEM, 0); mainForm.addCommand(exitCommand); mainForm.addCommand(historyCommand); mainForm.addCommand(mathsCommand); mainForm.setCommandListener(this); historyForm = new Form("History questions"); historyForm.append("When was the battle of Hastings?"); Command hRightCommand = new Command("1066", Command.HELP, 0); Command hWrongCommand = new Command("1966", Command.CANCEL, 0); historyForm.addCommand(hRightCommand); historyForm.addCommand(hWrongCommand); historyForm.setCommandListener(this); display.setCurrent(mainForm); }

18 Programming Handheld and Mobile devices 18 public void pauseApp () {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) { if (c.getCommandType()==Command.EXIT) notifyDestroyed(); else if (c.getCommandType()==Command.ITEM) ;//code to open Maths window else if (c.getCommandType()==Command.OK) display.setCurrent(historyForm);//open history window else; //default case }

19 Programming Handheld and Mobile devices 19

20 Programming Handheld and Mobile devices 20 Carrying on We can add the same Forms as we had in the Palm OS version We can experiment with other lcdui features We can use Alert to flag outcomes How does MIDP compare with Palm OS?


Download ppt "Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley"

Similar presentations


Ads by Google