Alarm with Timer and Alert : Timer « J2ME « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » J2ME » Timer 
31.21.2.Alarm with Timer and AlertPrevious/Next
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.midlet.MIDlet;

public class AlarmMIDlet extends MIDlet implements ItemStateListener, CommandListener {
  private Command alarmCommand= new Command("alarm", Command.SCREEN, 1);

  private Command resetCommand= new Command("Reset", Command.SCREEN, 1);

  private Command exitCommand= new Command("Exit", Command.EXIT, 1);

  private Display display;

  private DateField alarmTimeSetting;

  private Form aForm= new Form("Alarm Setting");

  private int dateIndex;

  private Date currentTime= new Date();

  private Timer aTimer;

  private boolean dateOK = false;

  public AlarmMIDlet() {
    display = Display.getDisplay(this);
    alarmTimeSetting = new DateField("", DateField.DATE_TIME);
    alarmTimeSetting.setDate(currentTime);

    dateIndex = aForm.append(alarmTimeSetting);
    aForm.addCommand(alarmCommand);
    aForm.addCommand(resetCommand);
    aForm.addCommand(exitCommand);
    aForm.setCommandListener(this);
    aForm.setItemStateListener(this);
  }

  public void startApp() {
    display.setCurrent(aForm);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void itemStateChanged(Item item) {
    if (item == alarmTimeSetting) {
      if (alarmTimeSetting.getDate().getTime() < currentTime.getTime())
        dateOK = false;
      else
        dateOK = true;
    }
  }

  public void commandAction(Command c, Displayable s) {
    if (c == alarmCommand) {
      if (dateOK == false) {
        Alert aAlert = new Alert("Cannot set""Warning", null, null);
        aAlert.setTimeout(Alert.FOREVER);
        aAlert.setType(AlertType.ERROR);
        display.setCurrent(aAlert);
      else {
        aTimer = new Timer();
        alarmTimerTask aTimerTask = new alarmTimerTask();
        long amount = alarmTimeSetting.getDate().getTime() - currentTime.getTime();
        aTimer.schedule(aTimerTask, amount);

        aForm.removeCommand(alarmCommand);
        aForm.removeCommand(resetCommand);
        aForm.delete(dateIndex);

        aForm.setTitle("Waiting....");
      }
    else if (c == resetCommand) {
      alarmTimeSetting.setDate(currentTime = new Date());
    else if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }

  class alarmTimerTask extends TimerTask {
    public final void run() {
      Alert aAlert = new Alert("Alert!");
      aAlert.setTimeout(Alert.FOREVER);
      aAlert.setType(AlertType.ALARM);
      AlertType.ERROR.playSound(display);
      display.setCurrent(aAlert);
      cancel();
    }
  }
}
31.21.Timer
31.21.1.Simple TimerSimple Timer
31.21.2.Alarm with Timer and Alert
31.21.3.Fixed Rate Schedule MIDletFixed Rate Schedule MIDlet
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.