Share on Google+Share on Google+

J2ME Timer MIDlet Example

This Example shows how to use of timer class.

J2ME Timer MIDlet Example


This Example shows how to use of timer class. In this example we are using the Timer class to create the time of execution  of application and the TimerTask class is used to create the counter of timer which is executed after 5000 ms as below:







private class TestTimerTask extends TimerTask{
  public final void run(){
  form.append("Timer Execute Count: " + ++count + "\n");

in the above source code we execute the counter of the timer on the mobile window as follows:


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

public class TimerMIDlet extends MIDlet implements CommandListener{
  private Display display;
  private Form form;
  private Command exit, stop; 
  private Timer timer; 
  private TestTimerTask task; 
  private int count = 0

  public TimerMIDlet(){
  display = Display.getDisplay(this);  
  form = new Form("Timer Example");
  exit = new Command("Exit", Command.EXIT, 1);
  stop= new Command("Stop", Command.STOP, 2);
  form.append("Please wait for timer.. \n");

  public void startApp (){
  timer = new Timer();
  task = new TestTimerTask();

  public void pauseApp (){ }

  public void destroyApp (boolean unconditional){

  public void commandAction(Command c, Displayable d){
  String label = c.getLabel();
  if (label.equals("Stop")){
  }else if (label.equals("Exit")) {

  private class TestTimerTask extends TimerTask{
  public final void run(){
  form.append("Timer Execute Count: " + ++count + "\n");

Download Source Code



Posted on: December 1, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: J2ME Timer MIDlet Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 23, 2013
timer program using j2me

July 31, 2014
Very useful!

Thanks dude!, Very useful!