Alert class inherits from SCREEN, and its role is to notify the user to have an abnormal information. There are three classes in the Advanced User Interface API that they can't have a parent, they must occupy the screen, one is Alert, and two are List and TextBox. In MIDP1.0, the flexibility of Alert is relatively poor, and the information indicated is comparable to the dead plate. Adding an important feature in MIDP2.0 is to add an indicator on the ALERT, so it can be used with Guage.
Guage used in conjunction with Alert 1. It must be non-interactive, 2. In addition to the other containers, 3.guage's Label must be NULL, 4.guage can't be associated with Command and CommandListener. For example, create a Guage instance int max = ... // initial value gauge gauge = ... // initial value gauge gauge = ... // initial value gauge = ... // initial value gauge gauge = new gauge (null, false, max, initial); Setindicator () Combine Alert and Guage Alert a = new alert ("my alert"); a.setindicator (gauge); because MIDP's user interface class is thread secure, you can change the value of Guage in other threads, Alert automatically resets the screen to update the current value of Guage.
In MIDP2.0, we can explicitly add Command to Alert, and of course, if you want to implement the CommandListener's CommandAction () method to tell the system what to do when Command is pressed. In MIDP1.0, there is a default Command and Alert association. If we add Command in Alert, then this default Command is replaced, if the added command is deleted, the default Command Alternatively recover and the association of ALERT. If we add more than two Command on Alert, then its Timeout is automatically set to Forever.
The following apps are well explained how to use different Alert to express different information.
Import javax.microedition.lcdui. *; import javax.microedition.midlet. *;
// a Simple Midlet for Testing Various Alerts.
Public Class AlertTest Extends MIDlet ImmandListener {
// An Abstract Class for Our Alert Tests.
Public Abstract Class Alertrunner {Protected Static Final Int One_Second = 1000; protected static final int frame_seconds = 5000;
Public alertrunner (String Title) {_title = title;}
Public Abstract Alert doalert ();
Public string gettitle () {return_title;}
Public string toString () {return gettitle ();
Private string_title;
// An Alert Test for a Simple Timed Alert.public Class Timedalert Extends Alertunner {Public Timedlert (SUPER) {Super (title)
Public Alert doalert () {Alert a = new alert (gettitle ()); a.setString ("Times Out After 5 Seconds ..."); a.SetTimeout (FIVE_SECONDS);
Showalert (a);
Return A;}}
// ALERT TEST for a Simple Modal Alert.
Public Class Modralrert Extends Alertunner {Public Modralrt (String Title) {Super (Title)
Public Alert doalert () {Alert a = new alert (GetTitle ()); A.SetString ("Waits to Be Dismble"); a.SetTimeout (Alert.Forever);
Showalert (a);
Return A;}}
// An Alert Test That Displays An Alert with // A Gauge Whose Value Is Increased Every Second. // The Alert Can Be Dismbled Only After The Gauge // Reaches Its Maximum Value.
Public Class Progressalert Extends Alertunner Imtribosite CommandListener, Runnable {
Private static final int max = 10;
Public Progressalert (String Title) {super (title);}
Public alert doalert () {_gauge = new gauge (null, false, max, 0); _Gauge.setValue (0); _done = false;
_alert = new alert (getTitle ()); _lert.setString ("Counts to" max "and the lets you dismiss it"); _lert.setcommandlistener (tris); _alert.setindicator (_Gauge);
// set a _vey_ long timeout _lert.settimeout (one_second * 3600);
Showalert; New Thread (this) .start ();
Return_alert;} public void CommandAction (Command C, Displayable D) {if (_done || _Gauge.getValue ()> = max) {showlist ();}}
Private void done () {_alert.addcommand (New Command ("DONE", Command.ok, 1); _done = true;
// a Thread this bumps the value of the counter // every second.
Public void run () {int val = _Gauge.getValue (); try {while (value (one_second); _Gauge.setValue ( Val);}} catch (interruptedExcection e) {}
DONE ();
Private alert_alert; private int _counter; private gauge _done;}
// An Alert Test Test That Displays a Continuously // Running Gauge Before Automatical Timing Out.
Public class busyalert extends alertunner {public busyalert (String title) {super (title);}
Public alert doalert () {_gauge = new gauge (null, false, gauge.indefinite, gauge.continuous_running);
_lert = new alert (GetTitle ()); _lert.setstring ("Runs for 5 seconds and" "Times out automatic"); _lert.setindicator (_Gauge); _alert.settimeout;
Showalert (_ALERT);
Return_alert;
Private alert_alert; private gauge_gauge;
// Standard MIDlet code. Displays a list of // available alert tests and runs the test // it's been chosen.
PRIVATE DISPLAY; Public Static Final Command EXITCOMMAND = New Command ("EXIT", Command.exit, 1);
Public alertTest () {}
public void commandAction (Command c, Displayable d) {if (c == exitCommand) {exitMIDlet ();} else if (c == List.SELECT_COMMAND) {int index = _alertList.getSelectedIndex (); _alertRunners [index] .doAlert ();
Protected Void DestroyApp (Boolean Unconditional) THROWS MIDLETSTATECHANGEXCEPTION {EXITMIDLET ();
Public void exitmidlet () {notifydestroyed ();
Public Display getDisplay () {Return Display;
protected void initmidlet () {
// the list of alert tests ....
_alertrunners = new alertrunner [] {new timedalert ("Timed Alert"), New Modralrt ("Modal Alert"), New Progressalert ("Progress Alert"), New Busyalert ("Busy Alert")}
_lertlist = new list ("Alert Testing", list.Implicit; _lertlist.setCommandListener (this);
For (int i = 0; i <_alertrunners.length; i) {_lertlist.Append (_alertrunners [i] .tostring (), null);
SHOWLIST ();
protected void pauseapp () {}
Private void Showalert (Alert a) {getDisplay (). setCurrent (a, _alertlist);}
Private void showlist () {getDisplay (). setcurrent (_LerthList);
Protected Void Startapp () throws MidletStateChangeException {if (Display == Null) {Display = Display.getDisplay (this); initmIdlet ();}}