Timer Template : Timer « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » J2ME » Timer 




Timer Template
Timer Template


/*--------------------------------------------------
* TimerTemplate.java
*
* Test all six Timer scheduling options
*
* Example from the book:     Core J2ME Technology
* Copyright John W. Muchow   http://www.CoreJ2ME.com
* You may use/modify for any non-commercial purpose
*-------------------------------------------------*/
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class TimerTemplate extends MIDlet implements CommandListener
{
  private Display display;      // Our display
  private Form fmMain;         // Main form
  private Command cmExit;      // Exit midlet
  private Command cmStop;      // Stop the timer
  private Timer tm;          // Timer
  private TestTimerTask tt;       // Task
  private int count = 0;        // How many times has task run

  public TimerTemplate()
  {
    display = Display.getDisplay(this);

    // Create main form
    fmMain = new Form("Timer Test");
    fmMain.append("waiting...\n");

    // Create commands and add to form
    cmExit = new Command("Exit", Command.EXIT, 1);
    cmStop= new Command("Stop", Command.STOP, 2);
    fmMain.addCommand(cmExit);
    fmMain.addCommand(cmStop);
    fmMain.setCommandListener(this);
    
    //------------------------------------------------------
    // Option #1 - One time task with delayed start
    // Create a timer that will go off in 5 seconds
    //------------------------------------------------------
    tm = new Timer();
    tt = new TestTimerTask();
    tm.schedule(tt,5000);    

    //------------------------------------------------------
    // Option #2 - Fixed-delay repeating task with delayed start
    // Create a timer that will go off in 5 seconds
    // Repeating every 3 seconds
    //------------------------------------------------------
//    tm = new Timer();
//    tt = new TestTimerTask();
//    tm.schedule(tt,5000, 3000);    

    //------------------------------------------------------
    // Option #3 - Fixed-rate repeating task with delayed start
    // Create a timer that will go off in 5 seconds
    // Repeating every 3 seconds
    //------------------------------------------------------
//    timer = new Timer();
//    tt = new TestTimerTask();
//    timer.scheduleAtFixedRate(tt,5000, 3000);    

    //------------------------------------------------------
    // Option #4 - One time task at specified date
    // Create timer that starts at current date
    //------------------------------------------------------
//    timer = new Timer();
//    tt = new TestTimerTask();
//    timer.schedule(tt, new Date());    

    //------------------------------------------------------
    // Option #5 - Fixed-delay repeating task starting 
    //             at a specified date
    // Create timer that starts at current date
    // Repeating every 3 seconds
    //------------------------------------------------------
//    timer = new Timer();
//    tt = new TestTimerTask();
//    timer.schedule(tt, new Date(), 3000);    

    //------------------------------------------------------
    // Option #6 - Fixed-rate repeating task starting
    //             at a specified date
    // Create timer that starts at current date
    // Repeating every 3 seconds
    //------------------------------------------------------
//    timer = new Timer();
//    tt = new TestTimerTask();
//    timer.scheduleAtFixedRate(tt, new Date(), 3000);    
  }

  /*--------------------------------------------------
  * Show the main Form
  *-------------------------------------------------*/
  public void startApp ()
  {
    display.setCurrent(fmMain);
  }

  /*--------------------------------------------------
  * Shutting down. Cleanup all we created
  *-------------------------------------------------*/
  public void destroyApp (boolean unconditional)
  {
  }

  /*--------------------------------------------------
  * No pause code necessary
  *-------------------------------------------------*/
  public void pauseApp ()
  { }

  /*--------------------------------------------------
  * Process events for the main form
  *-------------------------------------------------*/
  public void commandAction(Command c, Displayable d)
  {
    if (c == cmStop)
    {
      tm.cancel();
    }
    else if (c == cmExit)
    {
      destroyApp(false);
      notifyDestroyed();
    }
  }

  /*--------------------------------------------------
  * TestTimerTask Class - Run the task
  *-------------------------------------------------*/  
  private class TestTimerTask extends TimerTask
  {
    public final void run()
    {
      fmMain.append("run count: " + ++count + "\n");
    }
  }
}



           
       














Related examples in the same category
1.Timer and stickerTimer and sticker
2.Timer and animationTimer and animation
3.Demonstrate simple animation using a Timer and TimerTaskDemonstrate simple animation using a Timer and TimerTask
4.A simple class that shows an example of using a Timer and a TimerTask.A simple class that shows an example of using a Timer and a TimerTask.
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.