Title: Event Handling of MIDlet
1Event Handling of MIDlet
2Agenda
- Life Cycle of MIDlet
- Input Interface of Physical Machine
- Event Handling of MIDlet
3(No Transcript)
4Program Structure of MIDlet
- Your MIDlets must inherit from class
javax.microedition.midlet.MIDlet - Abstract methods in class MIDlet
- startApp() signals the MIDlet that it has
entered the Active state - pauseApp() signals the MIDlet to stop and enter
the Pause state - destroyApp() signals the MIDlet to terminate
and enter the Destroyed state
5Life Cycle of MIDlets
The methods are called by Java Application
Manager
Constructor New myMIDlet()
Pause
startApp()
destroyApp()
pauseApp()
Active
Destroyed
destroyApp()
6- MIDlet can also change its own state by
signaling Java Application Manager
Pause
notifyDestroyed()
resumeRequest()
notifyPause()
Active
Destroyed
notifyDestroyed()
7Basic Operating Procedure
JAM call startApp()
MIDlet entering Active state
Get the Display object
Set current screen to display
8import javax.microedition.midlet. public class
HelloMIDlet extends MIDlet implements
CommandListener public HelloMIDlet()
display Display.getDisplay(this)
public void startApp()
display.setCurrent(myForm)
public void pauseApp() public void
destroyApp(boolean unconditional)
9Input Interface in Physical Machines
- Different machines behaves differently
- Programmers does not have to worry about this
- KVM handles the differences and mappings
10Control Key
Navigation Key
Touch Panel On LCD Screen
Soft Button
Alphanumeric Key
11Event Handling
- Event
- Event Handler
- Callback method
- Interfaces in Java
12Interface in Java
- What Are Interfaces?
- An interface is a collection of method
definitions (without implementations) and
constant values - Defining an Interface
- Implementing an Interface
- Using an Interface as a Type
13What Are Interfaces?
- Useful for
- Capturing similarities between unrelated classes
without forcing a class relationship - Declaring methods that one or more classes are
expected to implement - Revealing an object's programming interface
without revealing its class - Interfaces do not provide multiple Inheritance
- Differences from multiple inheritance
- You cannot inherit variables from an interface
- You cannot inherit method implementations from an
interface. - The interface hierarchy is independent of a the
class hierarchy. This is not true for multiple
inheritance
14Defining an Interface
- The interface declaration Â
- Â public interface InterfaceName extendsÂ
listOfSuperInterfaces . . . - The interface body
- All constant values defined in an interface are
implicitly public, static, and final - All methods declared in an interface are
implicitly public and abstract - Interfaces and their methods are implicitly
abstract
15Implementing an Interface
- A class that implement the interface
- A class declares all of the interfaces that it
implements in its class declaration - A class implements one or more interfaces use the
key word implements followed by a comma-delimited
list of the interfaces implemented by your class
16Using an Interface as a Type
- You can use interface names anywhere you'd use
any primitive data type name or any other
reference data type name - An example
17interface CellAble    void draw()    void
toString() Â Â Â void toFloat()
class MyCell implements CellAble    void
draw() Â Â Â Â Â ... Â Â Â void toString()
... void toFloat() ...
class Row    private CellAble contents
           . . .    void setAt(CellAble ca,
int index) Â Â Â Â Â Â Â Â Â Â ca.draw() Â Â Â Â Â Â Â
... Â Â Â Â Â Â Â ca.toString() Â Â Â Â Â Â Â ... Â Â Â
18Types of Event Handling for MIDlet
- High-level event handling
- Most general events
- Low-level event handling
- Key pressed, or touch screen
- Event handling for painting event
- Runnables run() method
19Handling High Level Events
- Two high-level event handling interfaces in
MIDlet - CommandListener
- For Command in javax.microedition.lcdui
- commandAction(Command c, Displayable d)
- ItemStateListener
- For state changed, such as Item in
javax.microedition.lcdui - itemStateChanged(Item item)
- CommandListener
- An example HLEventMIDlet.java
20- Steps of event handling
- Register event handler
- Event occurs
- Event listener is notified, and then the event
handler is invoked - Unicast mechanism
- Only one listener can be registered to an event
source at the same time
21public HLEventMIDlet() display
Display.getDisplay(this) exitCommand new
Command("Exit", Command.SCREEN,1) info1Command
new Command("Info1", Command.SCREEN,2) info2Co
mmand new Command("Info2", Command.SCREEN,2)
3 commands
public void startApp() TextBox t new
TextBox("Hello MIDlet", "Test string", 256,
0) t.addCommand(exitCommand) t.addCommand(info
2Command) t.addCommand(info1Command) t.setCom
mandListener(this) display.setCurrent(t)
Add 3 commands
Set command listener
22implements the interface
public class HLEventMIDlet extends MIDlet
implements CommandListener public
void commandAction(Command c, Displayable s)
if (c exitCommand)
notifyDestroyed() else if (c info1Command)
notifyDestroyed()
Handling the event
23- Running the MIDlet using different phone
emulators - Three commands, how about 2 commands? 4 commands?
- new Command("Exit", Command.SCREEN,1)
Label to be displayed on screen
Command type
Command priority
24- Label of the command
- Command types
- Command.BACK, Command.CANCEL, Command.EXIT,
Command.HELP, Command.ITEM, Command.OK,
Command.SCREEN, Command.STOP - Priority of the command
25- Another example ListEventMIDlet.java
public void startApp() List l new
List("Test Item",Choice.IMPLICIT)
l.setCommandListener(this) l.append("Test
1",null) l.append("Test 2",null)
l.addCommand(exitCommand)
display.setCurrent(l)
public void commandAction() if (c
exitCommand) notifyDestroyed() else
List tmp (List) s switch(tmp.getSelectedI
ndex()) case 0 display.setCurrent(new
TextBox("Test1","Test1",256,0))
break case 1 display.setCurrent(new
TextBox("Test2","Test2",256,0))
break
26ItemStateListener
- Implements ItenStateListener
- setItemStateListener()
- ItemStateChanged()
- An example TFEventMIDlet.java
27public void startApp() Form f new
Form("Form") TextField tf new
TextField("Input","Origin",25,TextField.ANY)
f.append(tf) f.setItemStateListener(this)
display.setCurrent(f)
public class TFEventMIDlet extends MIDlet
implements ItemStateListener . public
void itemStateChanged(Item item)
TextField tmp (TextField) item
tmp.setString("Changed")
28Low-Level Event Handling
- Class Canvas
- Derived from class Displayable
- Command is allowed
- ItemState is not allowed
- Two kinds of low-level events
- Key events
- Pointer events
29- Key Event
- keyPressed(int keyCode)
- keyReleaseed(int keyCode)
- keyRepeated(int keyCode)
- keyCode
- KEY_NUM0, KEY_NUM1, KEY_NUM2, , KEY_NUM9,
KEY_STAR, KEY_POUND - An example LLEventMIDlet.java
30class MyCanvas extends Canvas String action
"" public void paint(Graphics g)
//???? g.setColor(255,255,255)
g.fillRect(0,0,getWidth(),getHeight())
g.setColor(0,0,0) if(hasRepeatEvents())
//?????????? g.drawString("Repeat",10,10,0)
else g.drawString("NonRepeat",10,10,0)
g.drawString(action,10,20,0)
protected void keyPressed(int keyCode)
action KP (char)keycode repaint()
protected void keyReleased(int keyCode) .
31- Game Design -- Key code
- Not all phone has the same key placement
- May cause problems in games
- Abstract key map for games
- UP, DOWN, LEFT, RIGHT, FIRE, GAME_A, GAME_B,
GAME_C, GAME_D - getGameAction(keyCode)
- Convert KeyCode to its corresponding GameCode
- getKeyCode(gameCode)
- Convert GameCode to its corresponding KeyCode
32- Pointer Event
- pointerPressed(int x, int y)
- pointerReleased(int x, int y)
- pointerDragged(int x, int y)
- hasPointerEvents() to check whether touch screen
is supported - An example TouchEventMIDlet.java
33if(hasPointerEvents()) g.drawString("Point",10,
10,0) else g.drawString("NonPoint",10,10,0)
protected void pointerPressed(int x,int y)
action "x" x "" "y" y
repaint() protected void pointerReleased(int
x,int y) action "x" x "" "y" y
repaint() protected void pointerDragged(int
x,int y) action "x" x "" "y" y
repaint()
34More Examples
- An Example Using Class Date
- DateExample.java
- Two listeners
- Class Date
- Selecting different display format
- Displaying date
35TextBox1
TextBox2
SetDateType DisplayDate Exit
SetUSAType SetNormalType Exit
- Set date format
- Set string to TextBox1
- Set current to TextBox1
1.Get date 2.Prepare string 3.Set string to
TextBox1
36- Random Number Generation
- RanGen2.java
- Class Random supports different methods in J2SE
and MIDP
37(No Transcript)
38- Lottery Using Random Number
- Lottery.java
- Application Properties
- Get Application Property
- Set Application Property
- String v.s. StringBuffer
Form
StringItem
More Exit