Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Accessing Commands Through a Soft Button

Let's begin with Listing 1, a simple example that defines just one command to facilitate exiting a MIDlet.

Listing 1: AccessingCommands.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class AccessingCommands extends MIDlet implements CommandListener
{
 private Display display;   // Reference to Display object
 private Form fmMain;    // Form
 private Command cmExit;   // Command to exit the MIDlet

 public AccessingCommands()
 {
  display = Display.getDisplay(this);

  cmExit = new Command("Exit", Command.EXIT, 1);

  fmMain = new Form("Core J2ME");
  fmMain.addCommand(cmExit);
  fmMain.setCommandListener(this);
 }

 // Called by application manager to start the MIDlet.
 public void startApp()
 {
  display.setCurrent(fmMain);
 }

 // A required method
 public void pauseApp()
 { }

 // A required method
 public void destroyApp(boolean unconditional)
 { }

 // Check to see if our Exit command was selected
 public void commandAction(Command c, Displayable s)
 {
  if (c == cmExit)
  {
   destroyApp(false);
   notifyDestroyed();
  }
 }
}

Inside the constructor, we create Command and Form objects, add the Command to the Form, and set an event listener:

  cmExit = new Command("Exit", Command.EXIT, 1);
  fmMain = new Form("Core J2ME");
  fmMain.addCommand(cmExit);
  fmMain.setCommandListener(this);

When the user interacts with the device, generating an event, the method commandAction() will be called.

public void commandAction(Command c, Displayable s)
    {
    if (c == cmExit)
    {
     destroyApp(false);
     notifyDestroyed();
    }
}

We check to see if the event was generated by selecting the Exit command. If so, we call destroyApp() to clean up any resources that we acquired and follow this with a call to notifyDestroyed() to tell the application manager that it is safe to shutdown this MIDlet. (See Figure 4.)

Figure 4 Exit command mapped to a soft button on the Nokia emulator.

Now let's take a look at the same MIDlet when run inside a pager emulator (see Figure 5).

Figure 5 Exit command mapped to a soft button.

The Exit command is not available through a soft button. To exit the MIDlet, we need to enable the menu by selecting the button highlighted in Figure 5. Once the menu appears, select the Exit menu item with the button highlighted in Figure 6.

Figure 6 Accessing the Exit command through a menu.

One of the objectives of MIDP is to support a wide range of devices with differences not only in processing power and memory, but also in size and hardware configuration. This MIDlet is a good example because it will run on two different devices with two different interfaces. Although the end result is the same, the pager placed the Exit command on a menu, whereas the Nokia emulator mapped the same command to a soft button.

NOTE

The actual implementation of a Command is defined not by the MIDP Specification, but by the base functionality. Put another way, how a Command is presented on the display—and whether it is mapped to a key or placed in a menu—is up to the device manufacturer.

  • + Share This
  • 🔖 Save To Your Account