Handling Events Using the MIDP Command Object
To greatly oversimplify, event handling is nothing more than recognizing when an event occurs and taking an action based on that event. For example, this might involve recognizing that a Help button has been pressed and displaying a help message. To break this down to the lowest level, there are three key steps to successfully managing an event:
The hardware (the physical device itself) must recognize that something has occurred, a button has been pressed, a button has been released, an adapter has been plugged in, and so forth.
The software on the device (the application manager) needs to be notified of the event.
This is the point where we come into the picture as MIDlet developers. A message from the application manager will be sent to the MIDlet. This message will contain information about the event so that we can make decisions on how to proceed.
Before a MIDlet can recognize a message from the application manager about an event, we must set up an event "listener." There are two listener interfaces available in the MID Profile: CommandListener and ItemStateListener. When you want to process events, you will need a class that implements one or both of these interfaces. As part of the class that implements the interface(s), you will write the commandAction() and/or itemStateChanged() methods, where you place the code to decipher what event occurred and how you would like to handle the event.