Timer and TimerTask example in J2ME
By: Emiley J. in J2ME Tutorials on 2008-07-07
This sample J2ME program demonstrates the use of Timer and TimerTask in a midlet. 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.
/* * 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.*; 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, 0, 1000); timer.schedule(timerTaskTwo, 0, 1500); 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(); } } }
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
GUI components and menu based J2ME Applications.
Code sample to Send SMS from a J2ME application.
Adding your own Application icon for your J2ME application (jar file)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Datagrams in J2ME (UDP Programming sample)
Client Server in J2ME (Socket Programming sample)
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Comments