Using Alerts

Team-Fly

An alert is an informative message shown to the user. In the MIDP universe, there are two flavors of alert:

  • A timed alert is shown for a certain amount of time, typically just a few seconds. It displays an informative message that does not need to be acknowledged, like "Your transaction is complete," or "I can't do that right now, Dave."

  • A modal alert stays up until the user dismisses it. Modal alerts are useful when you need to offer the user a choice of actions. You might display a message like "Are you ready to book these tickets?" and offer Yes and No commands as options.

MIDP alerts can have an associated icon, like a stop sign or question mark. Alerts may even have an associated sound, although this depends on the implementation. MIDP alerts are very much the same concept as modal dialogs in windowing systems like MacOS and Windows. Figure 5-6 shows a typical Alert.


Figure 5-6: Alerts are similar to modal dialogs in a desktop windowing system.

Alerts are represented by instances of the javax.microedition.lcdui.Alert class, which offers the following constructors:

 public Alert() public Alert(String title, String alertText, Image alertImage, AlertType alertType) 

Any or all of the parameters in the second constructor may be null.

By default, timed Alerts are created using a default timeout value; you can find out the default value by calling getDefaultTimeout(). To change the Alert's timeout, call setTimeout() with the timeout value in milliseconds. A special value, FOREVER, may be used to indicate that the Alert is modal.

You could create a simple timed Alert with the following code:

 Alert alert = new Alert("Sorry", "I'm sorry, Dave... ", null, null); 

To explicitly set the timeout value to five seconds, you could do this:

 alert.setTimeout(5000); 

If, instead, you wanted a modal alert, you would use the special value FOREVER:

 alert.setTimeout(Alert.FOREVER); 

The MIDP implementation will automatically supply a way to dismiss a modal alert. Sun's reference implementation, for example, provides a Done command mapped to a soft button.

Alert types serve as hints to the underlying MIDP implementation. The implementation may use the alert type to decide what kind of sound to play when the alert is shown. The AlertType class provides five types, accessed as static member variables: ALARM, CONFIRMATION, ERROR, INFO, and WARNING.

The following example, TwoAlerts, shows both types of alerts. It features a main TextBox that is displayed when the MIDlet begins. Two commands, Go and About, provide access to the alerts. The Go command shows a timed alert that contains a message about a fictitious network error. The About command displays a modal alert that could contain copyright information. A third command, Exit, provides a way to exit the MIDlet. Keep in mind that all three commands may not fit on the screen; some of them may be accessible from a secondary menu.

 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class TwoAlerts     extends MIDlet     implements CommandListener {   private Display mDisplay;   private TextBox mTextBox;   private Alert mTimedAlert;   private Alert mModalAlert;   private Command mAboutCommand, mGoCommand, mExitCommand;   public TwoAlerts() {     mAboutCommand = new Command("About", Command.SCREEN, 1);     mGoCommand = new Command("Go", Command.SCREEN, 1);     mExitCommand = new Command("Exit", Command.EXIT, 2);   }   public void startApp() {     mDisplay = Display.getDisplay(this);     mTextBox = new TextBox("TwoAlerts", " ", 1, TextField.ANY);     mTextBox.addCommand(mAboutCommand);     mTextBox.addCommand(mGoCommand);     mTextBox.addCommand(mExitCommand);     mTextBox.setCommandListener(this);     mTimedAlert = new Alert("Network error",         "A network error occurred. Please try again.",         null,         AlertType.INFO);     mModalAlert = new Alert("About TwoAlerts",         "TwoAlerts is a simple MIDlet that demonstrates the use of Alerts.",         null,         AlertType.INFO);     mModalAlert.setTimeout(Alert.FOREVER);     mDisplay.setCurrent(mTextBox);   }   public void pauseApp() {   }   public void destroyApp(boolean unconditional) {}   public void commandAction(Command c, Displayable s) {     if (c == mAboutCommand)       mDisplay.setCurrent(mModalAlert);     else if (c == mGoCommand)       mDisplay.setCurrent(mTimedAlert, mTextBox);     else if (c == mExitCommand)       notifyDestroyed();   } } 


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net