Home > Articles > Programming > Java

Handling Events Using the MIDP Command Object

  • Print
  • + Share This
J2ME author and trainer John Muchow discusses the basics of Mobile Information Device Profile applications (MIDlets). Event processing is essential to nearly every MIDlet—it's hard to imagine an application with no user interaction whatsoever, even if this interaction is as simple as requesting to start or stop a MIDlet. In this article, you learn the specifics of event handling using the MIDP Command object.
Portions of this article are derived from the book Core J2ME Technology and MIDP (Sun Microsystems Press and Prentice Hall PTR, 2002), by John W. Muchow.
Like this article? We recommend

Like this article? We recommend

The Basics

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:

  1. 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.

  2. The software on the device (the application manager) needs to be notified of the event.

  3. 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.

  • + Share This
  • 🔖 Save To Your Account