Fixed Rate Schedule MIDlet : Timer « J2ME « Java Tutorial






Fixed Rate Schedule MIDlet
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;

public class FixedRateScheduleMIDlet extends MIDlet implements CommandListener {

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

  private Display display;

  TextBox t= new TextBox("Time setting", "", 256, 0);

  private Timer aTimer= new Timer();

  private Date currentTime= new Date();

  private Calendar now = Calendar.getInstance();

  public FixedRateScheduleMIDlet() {
    display = Display.getDisplay(this);
    now.setTime(currentTime);
  }

  public void startApp() {
    ClockTimerTask aTimerTask = new ClockTimerTask();

    aTimer.schedule(aTimerTask, 1000, 1000);

    t.addCommand(exitCommand);
    t.setCommandListener(this);
    display.setCurrent(t);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }

  class ClockTimerTask extends TimerTask {
    public final void run() {
      t.setString("" + now.get(Calendar.HOUR) + ":" + now.get(Calendar.MINUTE) + ":"
          + now.get(Calendar.SECOND) + ":");
      display.setCurrent(t);
    }
  }

}








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