extends TimerTask : TimerTask « 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 » TimerTask 
31.22.1.extends TimerTask
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.Command;
import javax.microedition.lcdui.CommandListener;
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, 101000);

    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(00, width - 1, height - 1);
      g.setGrayScale(0);
      g.drawRect(00, width - 1, height - 1);

      g.drawString(nowString, 1010, 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":";

      ((ClockCanvasd).repaint();
    }
  }
}
31.22.TimerTask
31.22.1.extends TimerTaskextends TimerTask
31.22.2.Cancel a timer task
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.