AlertDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:6k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class AlertDemo
  4.     extends MIDlet {
  5.     private final static Command CMD_EXIT = new Command("Exit", Command.EXIT,1);
  6.     private final static Command CMD_SHOW = new Command("Show", Command.SCREEN,1);
  7.     private final static String[] typeStrings = {
  8.             "Alarm", "Confirmation", "Error", "Info", "Warning"
  9.         };
  10.     private final static String[] timeoutStrings = {
  11.             "2 Seconds", "4 Seconds", "8 Seconds", "Forever"
  12.         };
  13.     private Image[]imageArray;
  14.     private final static int SECOND = 1000;
  15.     private Display display;
  16.     private boolean firstTime = true;
  17.     private Form mainForm;
  18.     public AlertDemo() {
  19.         try{
  20.            imageArray = new Image[]{
  21.              Image.createImage("/alarm.png"),
  22.              Image.createImage("/confirmation.png"),
  23.              Image.createImage("/error.png"),
  24.              Image.createImage("/information.png"),
  25.              Image.createImage("/warning.png")
  26.            };
  27.         }catch(java.io.IOException e){
  28.              System.out.println(e.toString());
  29.         }
  30.         mainForm = new Form("Alert Options");
  31.     }
  32.     protected void startApp() {
  33.         display = Display.getDisplay(this);
  34.         showOption();
  35.     }
  36.     /**
  37.      * Creates the main display of the MIDlet.
  38.      * In this form the user will choose the properties of the alert
  39.      */
  40.     private void showOption() {
  41.         if (firstTime) {                
  42.             // choice-group for the type of the alert:
  43.             // "Alarm", "Confirmation", "Error", "Info" or  "Warning"
  44.             ChoiceGroup types = new ChoiceGroup("Type", ChoiceGroup.POPUP,
  45.                                                 typeStrings, null);
  46.             mainForm.append(types);
  47.             // choice-group for the timeout of the alert:
  48.             // "2 Seconds", "4 Seconds", "8 Seconds" or "Forever"
  49.             ChoiceGroup timeouts = new ChoiceGroup("Timeout", ChoiceGroup.POPUP,
  50.                                                    timeoutStrings, null);
  51.             mainForm.append(timeouts);
  52.             // a check-box to add an indicator to the alert
  53.             String[] optionStrings = { "Show Indicator" };
  54.             ChoiceGroup options = new ChoiceGroup("Options", Choice.MULTIPLE, 
  55.                                                 optionStrings, null);
  56.             mainForm.append(options);
  57.             mainForm.addCommand(CMD_SHOW);
  58.             mainForm.addCommand(CMD_EXIT);
  59.             mainForm.setCommandListener(
  60.             new AlertListener(types, timeouts, options));
  61.             firstTime = false;
  62.         }
  63.         display.setCurrent(mainForm);
  64.     }
  65.     private class AlertListener
  66.         implements CommandListener {
  67.         AlertType[] alertTypes = {
  68.             AlertType.ALARM, AlertType.CONFIRMATION, AlertType.ERROR, 
  69.             AlertType.INFO, AlertType.WARNING
  70.         };
  71.         ChoiceGroup typesCG;
  72.         int[] timeouts = { 2 * SECOND, 4 * SECOND, 8 * SECOND, Alert.FOREVER };
  73.         ChoiceGroup timeoutsCG;
  74.         ChoiceGroup indicatorCG;
  75.         public AlertListener(ChoiceGroup types, ChoiceGroup timeouts, 
  76.                             ChoiceGroup indicator) {
  77.             typesCG = types;
  78.             timeoutsCG = timeouts;
  79.             indicatorCG = indicator;
  80.         }
  81.         public void commandAction(Command c, Displayable d) {
  82.             if (c == CMD_SHOW) {
  83.                 int typeIndex = typesCG.getSelectedIndex();
  84.                 Alert alert = new Alert("Alert演示");
  85.                 alert.setType(alertTypes[typeIndex]);
  86.                 alert.setImage(imageArray[typeIndex]);//为Alert添加图片
  87.                 int timeoutIndex = timeoutsCG.getSelectedIndex();
  88.                 alert.setTimeout(timeouts[timeoutIndex]);
  89.                 alert.setString(
  90.                         typeStrings[typeIndex] + " Alert, Running " +
  91. timeoutStrings[timeoutIndex]);
  92.                 boolean[] SelectedFlags = new boolean[1];
  93.                 indicatorCG.getSelectedFlags(SelectedFlags);
  94.                 if (SelectedFlags[0]) {
  95.                     Gauge indicator = createIndicator(timeouts[timeoutIndex]);
  96.                     alert.setIndicator(indicator);
  97.                 }
  98.                 display.setCurrent(alert);
  99.             } else if (c == CMD_EXIT) {
  100.                 destroyApp(false);
  101.                 notifyDestroyed();
  102.             }
  103.         }
  104.     }
  105.     protected void destroyApp(boolean unconditional) {
  106.     }
  107.     protected void pauseApp() {
  108.     }
  109.     /**
  110.      * Creates the alert's indicator.
  111.      * If there is no timeout (maxValue == Alert.FOREVER), the indicator will be
  112.      * an "indefinite-running" gauge.
  113.      * If there is a timeout, the indicator will be a "non-interactive" gauge
  114.      * that is updated by a background thread.
  115.      */
  116.     private Gauge createIndicator(int maxValue) {
  117.         if (maxValue == Alert.FOREVER) {
  118.             return new Gauge(null, false, Gauge.INDEFINITE, 
  119.                              Gauge.CONTINUOUS_RUNNING);
  120.         }
  121.         final int max = maxValue / SECOND;
  122.         final Gauge indicator = new Gauge(null, false, max, 0);
  123.             new Thread() {
  124.                 public void run() {
  125.                     int value = 0;
  126.                     while (value < max) {
  127.                         indicator.setValue(value);
  128.                         ++value;
  129.                         try {
  130.                             Thread.sleep(1000);
  131.                         } catch (InterruptedException ie) {
  132.                          }
  133.                     }
  134.                 }
  135.             }.start();
  136.         return indicator;
  137.     }
  138. }