Java Tutorial/J2ME/TimerTask — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 04:58, 1 июня 2010

Cancel a timer task

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class SimpleTimerCancelMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Command scheduleCommand = new Command("schedule", Command.SCREEN, 1);
  private TextBox aTextBox = new TextBox("Setting", "Content", 20, 0);
  public static String aMessage = "";
  Display display;
  private Timer aTimer;
  private SimpleTimerTask aTimerTask;
  public SimpleTimerCancelMIDlet() {
    Display display = Display.getDisplay(this);
  }
  public void startApp() {
    aTextBox.addCommand(exitCommand);
    aTextBox.addCommand(scheduleCommand);
    aTextBox.setCommandListener(this);
    display.setCurrent(aTextBox);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c == scheduleCommand) {
      aTimer = new Timer();
      aTimerTask = new SimpleTimerTask();
      aTimer.schedule(aTimerTask, 500);
      aTextBox.setString(aMessage);
    } else if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}
class SimpleTimerTask extends TimerTask {
  public final void run() {
    cancel();
  }
}





extends TimerTask

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class J2MEFixedRateSchedule extends MIDlet {
  private Timer aTimer;
  private ClockTimerTask aTimerTask;
  Displayable d = new ClockCanvas();
  private Date currentTime= new Date();
  private Calendar now = Calendar.getInstance();
  private String nowString = "";
  public void startApp() {
    d.addCommand(new Command("Exit", Command.EXIT, 0));
    d.setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable s) {
        notifyDestroyed();
      }
    });
    now.setTime(currentTime);
    nowString = now.get(Calendar.HOUR) + ":" + now.get(Calendar.MINUTE) + ":"
        + now.get(Calendar.SECOND) + ":";
    aTimer = new Timer();
    aTimerTask = new ClockTimerTask();
    aTimer.scheduleAtFixedRate(aTimerTask, 10, 1000);
    Display.getDisplay(this).setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  class ClockCanvas extends Canvas {
    public void paint(Graphics g) {
      int width = getWidth();
      int height = getHeight();
      g.setGrayScale(255);
      g.fillRect(0, 0, width - 1, height - 1);
      g.setGrayScale(0);
      g.drawRect(0, 0, width - 1, height - 1);
      g.drawString(nowString, 10, 10, Graphics.TOP | Graphics.LEFT);
    }
  }
  class ClockTimerTask extends TimerTask {
    public final void run() {
      currentTime = new Date();
      now = Calendar.getInstance();
      now.setTime(currentTime);
      nowString = now.get(Calendar.HOUR) + ":" + now.get(Calendar.MINUTE) + ":"
          + now.get(Calendar.SECOND) + ":";
      ((ClockCanvas) d).repaint();
    }
  }
}