Timer Template : Timer « J2ME « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » J2ME » TimerScreenshots 
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.