Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Session2-J2ME development-environment

1.847 Aufrufe

Veröffentlicht am

J2ME development-environment

  • Gehören Sie zu den Ersten, denen das gefällt!

Session2-J2ME development-environment

  1. 1. Outline <ul><li>MIDP </li></ul><ul><li>Building J2ME Apps- Tool </li></ul><ul><li>J2ME Wireless Toolkit Demo </li></ul><ul><li>MIDlet Programming </li></ul><ul><li>-- MIDlet Transition States </li></ul><ul><li>-- Midlet Skeleton </li></ul><ul><li>-- Two Level API </li></ul><ul><li>-- Displaying Objects </li></ul><ul><li>-- First Example – HelloWorld </li></ul><ul><li>-- Event Handling with Command s </li></ul><ul><li>-- Command Example </li></ul>
  2. 2. Mobile Information Device Profile (MIDP) <ul><li>Is a set of APIs that allow developers to control mobile device-specific problems </li></ul><ul><ul><li>i.e. user interfaces, local storage and client application lifecycles etc. </li></ul></ul><ul><li>MIDlets minimum requirements </li></ul><ul><ul><li>96 x 54 pixels mono screen </li></ul></ul><ul><ul><li>two-way wireless network </li></ul></ul><ul><ul><li>input device (i.e. keypad) </li></ul></ul><ul><ul><li>128 KB ROM for CLDC/MIDP class and another 32 KB RAM for the KVM </li></ul></ul><ul><li>Midlets are the most important and popular applications in the J2ME family. </li></ul>
  3. 3. MIDP
  4. 4. Building J2ME Apps- Tool <ul><li>Sun Java Wireless Toolkit 2.x for CLDC (The newest version is 2.5.2 in Jan 2008) which can be downloaded from </li></ul><ul><ul><li>http://java.sun.com/j2me/download.html </li></ul></ul>
  5. 5. J2ME Wireless Toolkit Demo <ul><li>Launch the Wireless Toolkit: </li></ul><ul><ul><li>Start > Programs > Sun Java(TM) Wireless Toolkit 2.5.2 for CLDC </li></ul></ul><ul><li>WTK already includes a set of demo programs ready to run. </li></ul>
  6. 6. J2ME Wireless Toolkit Demo <ul><li>Select menu item File > Open Project ... </li></ul><ul><li>Select UIDemo and click Open Project . </li></ul><ul><li>The projects can be used as the templates of your applications. </li></ul>
  7. 7. J2ME Wireless Toolkit Demo <ul><li>Click the Build and then the Run buttons. </li></ul>
  8. 8. J2ME Wireless Toolkit Demo <ul><li>The main menu screen is shown up. You can choose a program and select Launch to start the program. </li></ul>
  9. 9. MIDlet Programming <ul><li>Any MIDP application must extends MIDlet </li></ul><ul><li>This is the MIDP equivalent of an applet, where starting/stopping is under the control of the environment </li></ul><ul><li>Like Java applets, MIDlets have an application life cycle while running on a mobile device. </li></ul>
  10. 10. MIDlet Transition States <ul><li>Specifically, a MIDlet can be in one of three states as shown: </li></ul>Why do we need a Paused state?
  11. 11. Midlet Skeleton import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MyApp extends MIDlet { public void startApp() { // start up code } public void pauseApp() { // we aren't showing any more } public void destroyApp(boolean unconditional) { // clean up } } Note that startApp(), pauseApp() and destroyApp() are abstract methods. Midlet program must override these 3 methods even if not used.
  12. 12. Two Level API <ul><li>There are two areas of the API which you should be concerned with - the high and low-level API. </li></ul><ul><li>High-Level Provides input elements such as, </li></ul><ul><ul><li>text fields, choices, and form </li></ul></ul><ul><li>Low-level is for drawing on Canvases and capturing keyed events </li></ul><ul><li>All MIDlet applications need to import the necessary midlet and lcdui packages: </li></ul><ul><ul><li>import javax.microedition.midlet.*; </li></ul></ul><ul><ul><li>import javax.microedition.lcdui.*; </li></ul></ul>
  13. 13. Displaying Objects <ul><li>High-level Screens have a base class called Displayable . </li></ul><ul><li>To show something on a MIDP device, you need to obtain the device’s display </li></ul><ul><ul><li>javax.microedition.lcdui.Display </li></ul></ul><ul><li>This Display class is the one and only display manager for each active MIDlet and provides information about the device’s display capability. </li></ul><ul><li>Subclassed Displayable classes will fill the whole screen </li></ul>
  14. 14. Displaying Objects <ul><li>To show a Displayable object you must use the setCurrent() method on the Display object. </li></ul><ul><ul><li>Form mainForm = new Form (&quot; First Program &quot;); </li></ul></ul><ul><ul><li>Display display = Display.getDisplay(this); </li></ul></ul><ul><ul><li>display .setCurrent (mainForm); </li></ul></ul><ul><ul><li>Note that Form is a Displayable subclass. </li></ul></ul>
  15. 15. First Example - HelloWorld <ul><li>import javax.microedition.midlet.*; </li></ul><ul><li>import javax.microedition.lcdui.*; </li></ul><ul><li>public class HelloWorld extends MIDlet { </li></ul><ul><li>public HelloWorld() { } </li></ul><ul><li>public void startApp() { </li></ul><ul><li>Form form = new Form( &quot;First Program&quot; ); </li></ul><ul><li>form.append( &quot;Hello World&quot; ); </li></ul><ul><li>Display.getDisplay(this).setCurrent ( form ); </li></ul><ul><li>} </li></ul><ul><li>public void pauseApp() {} </li></ul><ul><li>public void destroyApp( boolean unconditional ) {} </li></ul><ul><li>} </li></ul>
  16. 16. Building the MIDlet <ul><li>Run the program KToolbar </li></ul><ul><li>Start > Programs > J2ME Wireless Toolkit 2.x > KToolbar </li></ul><ul><li>Click on New Project and e nter the Project name and class name as shown below </li></ul>
  17. 17. Building the MIDlet <ul><li>After pressing the Create Project Button, a directory tree will be created for the project: </li></ul>
  18. 18. Building the MIDlet <ul><li>Use TextPad to create a source file HelloWorld.java and save it under the directory src . </li></ul>
  19. 19. Building and Run the MIDlet <ul><li>Click the Build and then the Run buttons. </li></ul>
  20. 20. How can the program exit? <ul><li>The program can not exit unless you close the emulator. </li></ul><ul><li>To provide a way to exit the program, you need to use Commands. </li></ul><ul><li>A command is l ike a button, it has a title, like &quot;OK&quot; or &quot;Cancel,&quot; and your application can respond appropriately when the user invokes the command. </li></ul>
  21. 21. Event Handling with Command s <ul><li>Displayable, the parent of all screen displays, supports Commands . </li></ul><ul><li>The device determines how the commands are shown on the screen or invoked by user. </li></ul><ul><li>Every Displayable keeps a list of its Commands. You can add and remove Commands using the following methods: </li></ul><ul><ul><li>public void addCommand(Command cmd) </li></ul></ul><ul><ul><li>public void removeCommand(Command cmd) </li></ul></ul>
  22. 22. Command O bjects <ul><li>In J2ME, commands are commonly represented with soft-buttons on the device. The following diagram shows two Command objects, one with the label &quot;Exit&quot; and one with label &quot;View.&quot; </li></ul>soft-buttons
  23. 23. Command O bjects <ul><li>If there are too many commands to be shown on the display, a device will create a menu to hold multiple commands. The following diagram shows how this might look. </li></ul>
  24. 24. Use Command objects <ul><li>The basic steps to process events with a Command object are as follows: </li></ul><ul><ul><li>Create a Command object. </li></ul></ul><ul><ul><li>Add the Command to a Form (or other GUI objects TextBox, List, or Canvas ) . </li></ul></ul><ul><ul><li>Create and set a listener for the Form. </li></ul></ul><ul><li>Upon detection of an event, the listener will call the method commandAction(). </li></ul>
  25. 25. Create a Command <ul><li>To create a Command, you need to supply a label , a type, and a priority. </li></ul><ul><li>The type is used to signify a commonly used command. It helps device to arrange the commands. </li></ul>Command Meaning BACK returns to the previous screen. CANCEL standard negative answer to a dialog EXIT for exiting from the application. HELP a request for on-line help. ITEM specific to the items of the Screen or the elements of a Choice. OK standard positive answer to a dialog SCREEN an application-defined command STOP A command that will stop some currently running process, operation, etc.
  26. 26. Create a Command <ul><li>To create a standard OK command, for example, you would do this: </li></ul><ul><ul><li>Command c = new Command(&quot;OK&quot;, Command.OK, 0); </li></ul></ul><ul><li>To create a command specific to your application, you might do this: </li></ul><ul><ul><li>Command c = new Command( </li></ul></ul><ul><ul><li>&quot;Launch&quot;, Command.SCREEN, 0); </li></ul></ul>label type priority
  27. 27. P riority and Long Label <ul><li>Every command has a priority. </li></ul><ul><li>Lower numbers indicate a higher priority. </li></ul><ul><li>If you add a command with priority 0, then several more with priority 1, the priority 0 command will show up on the screen directly. The other commands will most likely end up in a secondary menu. </li></ul><ul><li>MIDP also support s for long labels on commands. </li></ul><ul><li>You can create a command with a short and long label like this: </li></ul><ul><li>Command c = new Command(&quot;Run&quot;, &quot;Run simulation&quot; , Command.SCREEN, 0); </li></ul><ul><li>The device decides which label it will use based on the available screen space and the size of the labels. </li></ul>
  28. 28. Responding to Commands <ul><li>Commands show up on the screen, but nothing happens automatically when a user invokes a command. </li></ul><ul><li>You need to write an object called a listener which will be called when the user invokes any command in a Displayable. </li></ul><ul><li>The listener is an object that implements the CommandListener interface. </li></ul><ul><li>To register the listener with a Displayable, use the following method: </li></ul><ul><ul><li>public void setListener (CommandListener l) </li></ul></ul><ul><li>Note it is one Listener per Displayable, NOT one Listener per one Command. </li></ul>
  29. 29. Example <ul><li>import javax.microedition.midlet.*; </li></ul><ul><li>import javax.microedition.lcdui.*; </li></ul><ul><li>public class Commander extends MIDlet implements CommandListener { </li></ul><ul><li>public void startApp() { </li></ul><ul><li>Displayable d = new Form( &quot;Test Command&quot; ); </li></ul><ul><li>Command c = new Command(&quot;Exit&quot;, Command.EXIT, 0); </li></ul><ul><li>d.addCommand(c); </li></ul><ul><li>d.setCommandListener(this); </li></ul><ul><li>Display.getDisplay(this).setCurrent(d); </li></ul><ul><li>} </li></ul><ul><li>public void pauseApp() { } </li></ul><ul><li>public void destroyApp(boolean unconditional) { } </li></ul><ul><li>public void commandAction ( Command c, Displayable s) { </li></ul><ul><li>notifyDestroyed(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Abstract method of CommandListener. Will be called when any command in the Form is selected.
  30. 31. Another Command Example (Two Forms) Launch Exit Exit 2nd Form Go to First Form
  31. 32. Another Command Example (Two Forms) <ul><li>import javax.microedition.lcdui.*; </li></ul><ul><li>import javax.microedition.midlet.*; </li></ul><ul><li>public class Commander2 extends MIDlet implements CommandListener { </li></ul><ul><li>Display display = null; </li></ul><ul><li>Form f1 = null; </li></ul><ul><li>Form f2 = null; </li></ul><ul><li>Command firstFormCommand = </li></ul><ul><li> new Command(&quot;1st Form&quot;, &quot;Go to First Form&quot;, Command.SCREEN, 0); </li></ul><ul><li>Command secondFormCommand = </li></ul><ul><li> new Command(&quot;2nd Form&quot;, &quot;Go to Second Form&quot;, Command.SCREEN, 0); </li></ul><ul><li>Command exitCommand = </li></ul><ul><li> new Command(&quot;Exit&quot;, Command.EXIT, 1); </li></ul>
  32. 33. Another Command Example (Two Forms) <ul><li>public void startApp() { </li></ul><ul><li>display = Display.getDisplay(this); </li></ul><ul><li>f1 = new Form( &quot;Form 1&quot; ); </li></ul><ul><li>f1.append( &quot;This is Form No. 1&quot; ); </li></ul><ul><li>f1.addCommand(secondFormCommand); </li></ul><ul><li>f1.addCommand(exitCommand); </li></ul><ul><li>f1.setCommandListener( this ); </li></ul><ul><li>f2 = new Form( &quot;Form 2&quot; ); </li></ul><ul><li>f2.append( &quot;This is Form No. 2&quot; ); </li></ul><ul><li>f2.addCommand(firstFormCommand); </li></ul><ul><li>f2.addCommand(exitCommand); </li></ul><ul><li>f2.setCommandListener(this); </li></ul><ul><li>display.setCurrent( f1 ); </li></ul><ul><li>} </li></ul>
  33. 34. Another Command Example (Two Forms) <ul><li>public void pauseApp() { } </li></ul><ul><li>public void destroyApp(boolean unconditional) { } </li></ul><ul><li>public void commandAction(Command c, Displayable d) { </li></ul><ul><li>if (c==exitCommand) </li></ul><ul><li> notifyDestroyed(); </li></ul><ul><li>else if (c==firstFormCommand) </li></ul><ul><li> Display.getDisplay(this).setCurrent( f1 ); </li></ul><ul><li>else </li></ul><ul><li> Display.getDisplay(this).setCurrent( f2 ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  34. 35. Simple Debugging <ul><li>System.out .print and System.out .println can be used for debugging. </li></ul><ul><li>When run in the simulator, the output is put on the console, not the phone . </li></ul>public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c==exitCommand) notifyDestroyed(); else if (c==firstFormCommand) { Display.getDisplay(this).setCurrent( f1 ); System.out.println(“1st Form is called&quot;); } else { System.out.println(&quot;2nd Form is called&quot;); Display.getDisplay(this).setCurrent( f2 ); } } }