- Understanding the Java WTK
- The RTTTLEx Application
- Creating a List of Ring Tones
- Understanding the MIDP List Class
- Working with the Display Object and the Displayable Interface
- Configuring Commands
- Registering CommandListeners
- The Methods of a MIDlet
- Selecting a Tune
- The MIDP Canvas Object
- Playing a Tune
- Conclusion
Configuring Commands
We now have the list of tunes displayed on the cell phone's screen. But how do we tell the app to play a tune when that tune is selected from the list? This is where understanding MIDP commands comes in.
You execute commands for an MIDP application by creating Command objects and associating them with an object that implements the Displayable interface. The Displayable interface's addCommand() method accomplishes this goal. These commands are executed by a CommandListener object that you register to the Displayable interface by using the setCommandListener() method.
Take a look at Listing 3, from the RTTTLEx application. This code creates two Command objects: one for an exit command and the other for a play command.
Listing 3 Creating Command Objects
private Command exitCommand = new Command("Exit",Command.EXIT, 1); private Command playCommand = new Command("Play",Command.ITEM, 1);
Two constructors are available for the Command object. Listing 3 uses the first available constructor, in which the parameters are
Command(String label, int commandtype, int priority)
The second constructor adds a fourth parameter and distinguishes between a short label and a long label. We won't use this second type of constructor in this article; read the documentation for the Command object for details.
After I create the Command objects, I add them to the RTTTLEx application in the RTTTLEx class constructor, as shown in Listing 4, using the commands created in Listing 3. In this case, the application's List object, theList, is the object that implements the Displayable interface.
Listing 4 Adding Commands to a List Object
//Add Command objects to the List object theList.addCommand(playCommand); theList.addCommand(exitCommand);
The important thing to notice about the Command object creation code is that the code in Listing 4 creates two different types of Command objects: one of command type Command.EXIT and the other of command type Command.ITEM. The difference is very important when displaying user choices on the mobile device's screen. Think of Command.EXIT as a predefined known type. A mobile device that supports MIDP knows how to display a Command.EXIT, but each device has its own way of displaying these commands. You don't have to do the programming to negotiate command placement in the user interface; WTK does it for you. There are other predefined types: BACK, CANCEL, EXIT, HELP, OK, SCREEN, and STOP. In this example, an Exit command is assigned to the left button above the keypad (see Figure 5).
Figure 5 Most cell phones automatically accommodate the placement of a command of type Command.EXIT.
Commands of type Command.ITEM are not predefined; displaying them requires a little more attention when programming. The default behavior for Command.ITEM assignments is covered extensively in the WTK documentation. You can read up on the specifics of command handling, if you're interested in learning more.