A simple class that shows an example of using a Timer and a TimerTask. : 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 
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.


/*
 * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
 */

import java.lang.*;
import java.io.*;
import java.util.*;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * A simple class that shows an example of using a Timer and
 * a TimerTask.
 *
 * This MIDlet creates two gauges. One gauge gaugeOne,
 * sets elements from low to high. The other, gaugeTwo,
 * set elements from high to low. In effect, this has
 * gaugeOne "going up", and gaugeTwo "going down."
 *
 * The two timers fire at different intervals.
 *
 * There are two commands on our form:
 
 * OK: toggles whether the times are active or not.
 * EXIT: exits the MIDlet.
 */

public class TimerMIDlet extends MIDlet implements CommandListener {
    // number of elements in gauge
    final private static int GAUGE_MAX = 10;

    private boolean timersRunning; // tracks state of timers
    private Display myDisplay;     // handle to the display

    private Gauge gaugeOne;        // "going up" gauge
    private Gauge gaugeTwo;        // "going down" gauge

    private Form myScreen;         // form on which to 
                                   // place gauges
    private Command cmdOK;         // OK command
    private Command cmdExit;       // EXIT command

    private Timer timer;
    private MyTimerTask timerTaskOne;
    private MyTimerTask timerTaskTwo;

    /**
     * Internal class that provides a TimerTask.
     */
    private class MyTimerTask extends TimerTask {
        private Gauge myGauge; // reference to gauge
        private boolean goUp;  // if true, go up
        private int num;       // number of times called

        /**
         * Public constructor: stores "direction" and a reference to
         * a gauge.
         */
        public MyTimerTask(Gauge g, boolean up) {
            myGauge = g;
            goUp = up;
        }

        /**
         * As the timer fires, this method is invoked. Set gauge
         * based on goUp
         */
        public void run() {
            num++;
            myGauge.setValue(goUp ?
                             GAUGE_MAX -(num % GAUGE_MAX:
                             num % GAUGE_MAX);
        }
    }

    /**
     * Public constructor: gets handle to display,
     * creates form, gauges, and commands.
     */
    public TimerMIDlet() {
        myDisplay = Display.getDisplay(this);
        myScreen = new Form("TimerMIDlet");
        gaugeOne = new Gauge("Up Gauge",
                             false,
                             GAUGE_MAX,
                             0);
        myScreen.append(gaugeOne);

        gaugeTwo = new Gauge("Down Gauge",
                             false,
                             GAUGE_MAX,
                             GAUGE_MAX);
        myScreen.append(gaugeTwo);

        cmdOK = new Command("OK", Command.OK, 1);
        cmdExit = new Command("Exit", Command.EXIT, 1);
        myScreen.addCommand(cmdOK);
        myScreen.addCommand(cmdExit);
        myScreen.setCommandListener(this);
    }

    /**
     * Changes the state of timers to/from active to/from
     * not-active.
     */
    private void flipFlop() {
        if (timersRunning) {
            timerTaskOne.cancel();
            timerTaskTwo.cancel();
            timer.cancel();
            timersRunning = false;
        else {
            timer = new Timer();
            timerTaskOne = new MyTimerTask(gaugeOne, false);
            timerTaskTwo = new MyTimerTask(gaugeTwo, true);
            timer.schedule(timerTaskOne, 01000);
            timer.schedule(timerTaskTwo, 01500);
            timersRunning = true;
        }
    }

    /**
     * Called by the system to start our MIDlet.
    @exception MIDletStateChangeException
     */
    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(myScreen);
        flipFlop();
    }


    /**
     * Called by the system to pause our MIDlet.
     * No actions required by our MIDLet.
     */
    protected void pauseApp() {}

    /**
     * Called by the system to end our MIDlet.
     * No actions required by our MIDLet.
     */
    protected void destroyApp(boolean unconditional) {}

    /***
     * Respond to command selections. Process two commands:
     
     * OK: flip flop the timers to/from active
     * EXIT: exit this MIDlet
     
     */
    public void commandAction(Command c, Displayable d) {
        if (c == cmdOK) {
            flipFlop();
        else if (c == cmdExit) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}


           
       
Related examples in the same category
1. Timer and stickerTimer and sticker
2. Timer and animationTimer and animation
3. Timer TemplateTimer Template
4. Demonstrate simple animation using a Timer and TimerTaskDemonstrate simple animation using a Timer and 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.