Alert类继承自Screen,它的作用是通知用户发生异常信息。在高级用户界面API中有三个类是不能有父容器的,他们必须要占据屏幕,其中一个就是Alert,另外两个是List和TextBox。在MIDP1.0中,Alert的灵活性比较差,表示的信息比较死板。在MIDP2.0中添加了一个重要的特性就是能够在Alert上添加指示器,因此可以和Guage结合起来使用。

    和Alert结合使用的Guage有以下要求1.必须是非交互性的,2.不能同时在其他的容器内,3.Guage的label必须为null,4.Guage不能和Command和CommandListener关联。比如按照如下方式创建一个Guage实例
 int max = ... // maximum value
 int initial = ... // initial value
 Gauge gauge = new Gauge( null, false, max, initial );
通过调用方法setIndicator()可以把Alert和Guage结合起来
 Alert a = new Alert( "My alert" );
 a.setIndicator( gauge );
由于MIDP的用户界面类都是线程安全的,因此你可以在其他的线程内改变guage的值,Alert会自动的重新绘制屏幕来更新guage的当前值。

    在MIDP2.0中,我们可以显式的对Alert添加Command,当然如果你还要实现CommandListener的commandAction()方法来告诉系统当Command 被按下的时候该做什么。在MIDP1.0中,有个默认的Command和Alert关联在一起的,如果我们在Alert中显式的添加了Command的话,那么这个默认的Command就被取代,如果添加的Command被删除,默认的Command会自动恢复和Alert的关联。如果我们在Alert 上添加了两个以上的Command,那么它的Timeout会自动设置为FOREVER。

    下面的应用程序很好的说明了如何使用不同的Alert表示不同的信息

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

// A simple MIDlet for testing various alerts.

public class AlertTest extends MIDlet implements CommandListener
{

    // An abstract class for our alert tests.

    public abstract class AlertRunner
    {
        protected static final int ONE_SECOND = 1000;
        protected static final int FIVE_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 AlertRunner
    {
        public TimedAlert(String title)
        {
            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;
        }
    }

    // An alert test for a simple modal alert.

    public class ModalAlert extends AlertRunner
    {
        public ModalAlert(String title)
        {
            super(title);
        }

        public Alert doAlert()
        {
            Alert a = new Alert(getTitle());
            a.setString("Waits to be dismissed");
            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 dismissed only after the gauge
    // reaches its maximum value.

    public class ProgressAlert extends AlertRunner implements 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());
            _alert.setString("Counts to " + MAX
                    + " and then lets you dismiss it");
            _alert.setCommandListener(this);
            _alert.setIndicator(_gauge);

            // Set a _very_ long timeout
            _alert.setTimeout(ONE_SECOND * 3600);

            showAlert(_alert);
            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 that bumps the value of the counter
        // every second.

        public void run()
        {
            int val = _gauge.getValue();
         
            try
            {
                while (val < MAX)
                {
                    Thread.sleep(ONE_SECOND);
                    _gauge.setValue(++val);
                }
            } catch (InterruptedException e)
            {
            }

            done();
        }

        private Alert _alert;
        private int _counter;
        private boolean _done;
        private Gauge _gauge;
    }

    // An alert test that displays a continuously
    // running gauge before automatically timing out.

    public class BusyAlert extends AlertRunner
    {
        public BusyAlert(String title)
        {
            super(title);
        }

        public Alert doAlert()
        {
            _gauge = new Gauge(null, false, Gauge.INDEFINITE,
                    Gauge.CONTINUOUS_RUNNING);

            _alert = new Alert(getTitle());
            _alert.setString("Runs for 5 seconds and "
                    + "times out automatically");
            _alert.setIndicator(_gauge);
            _alert.setTimeout(FIVE_SECONDS);

            showAlert(_alert);

            return _alert;
        }

        private Alert _alert;
        private Gauge _gauge;
    }

    // Standard MIDlet code. Displays a list of
    // available alert tests and runs the test once
    // it's been chosen.

    private Display 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 MIDletStateChangeException
    {
        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 ModalAlert("Modal alert"),
                new ProgressAlert("Progress alert"),
                new BusyAlert("Busy alert") };

        _alertList = new List("Alert Testing", List.IMPLICIT);
        _alertList.setCommandListener(this);

        for (int i = 0; i < _alertRunners.length; ++i)
        {
            _alertList.append(_alertRunners[i].toString(), null);
        }

        showList();
    }

    protected void pauseApp()
    {
    }

    private void showAlert(Alert a)
    {
        getDisplay().setCurrent(a, _alertList);
    }

    private void showList()
    {
        getDisplay().setCurrent(_alertList);
    }

    protected void startApp() throws MIDletStateChangeException
    {
        if (display == null)
        {
            display = Display.getDisplay(this);
            initMIDlet();
        }
    }

    private List _alertList;
    private AlertRunner[] _alertRunners;
}