Mega Code Archive

 
Categories / Java Tutorial / J2ME
 

Cancel a timer task

import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; public class SimpleTimerCancelMIDlet extends MIDlet implements CommandListener {   private Command exitCommand = new Command("Exit", Command.EXIT, 1);   private Command scheduleCommand = new Command("schedule", Command.SCREEN, 1);   private TextBox aTextBox = new TextBox("Setting", "Content", 20, 0);   public static String aMessage = "";   Display display;   private Timer aTimer;   private SimpleTimerTask aTimerTask;   public SimpleTimerCancelMIDlet() {     Display display = Display.getDisplay(this);   }   public void startApp() {     aTextBox.addCommand(exitCommand);     aTextBox.addCommand(scheduleCommand);     aTextBox.setCommandListener(this);     display.setCurrent(aTextBox);   }   public void pauseApp() {   }   public void destroyApp(boolean unconditional) {   }   public void commandAction(Command c, Displayable s) {     if (c == scheduleCommand) {       aTimer = new Timer();       aTimerTask = new SimpleTimerTask();       aTimer.schedule(aTimerTask, 500);       aTextBox.setString(aMessage);     } else if (c == exitCommand) {       destroyApp(false);       notifyDestroyed();     }   } } class SimpleTimerTask extends TimerTask {   public final void run() {     cancel();   } }