Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

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:

 

 

 

TimerMIDlet.java

 
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");
    form.addCommand(exit);
    form.addCommand(stop);
    form.setCommandListener(this);    
  }  

  public void startApp (){
    timer = new Timer();
    task = new TestTimerTask();
    timer.schedule(task,5000);
    display.setCurrent(form);
  }  

  public void pauseApp (){ }

  public void destroyApp (boolean unconditional){
    notifyDestroyed();
  }  

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

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

Download Source Code

                         

» View all related tutorials
Related Tags: c string image text io colors user color screen display click source cli message set create define show tex example

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.