📄 timertest.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
/**
* A MIDlet which demonstrates a Timer in action.
* @author Martin J. Wells
*/
public class TimerTest extends javax.microedition.midlet.MIDlet
{
private Form form;
private Timer timer;
private PrintTask task;
/**
* MIDlet constructor that creates a form, timer and simple task. See the
* inner class PrintTask for more information on the task we'll be executing.
*/
public TimerTest()
{
form = new Form("Timer Test");
// Setup the timer and the print timertask
timer = new Timer();
task = new PrintTask();
}
/**
* Called by the Application Manager when the MIDlet is starting or resuming
* after being paused. In this example it acquires the current Display object
* and uses it to set the Form object created in the MIDlet constructor as
* the active Screen to display.
* @throws MIDletStateChangeException
*/
protected void startApp() throws MIDletStateChangeException
{
// display our UI
Display.getDisplay(this).setCurrent(form);
// schedule the task for execution every 100 milliseconds
timer.schedule(task, 1000, 1000);
}
/**
* Called by the MID's Application Manager to pause the MIDlet. A good
* example of this is when the user receives an incoming phone call whilst
* playing your game. When they're done the Application Manager will call
* startApp to resume. For use this in order to stop the timer from running.
*/
protected void pauseApp()
{
task.cancel();
}
/**
* Called by the MID's Application Manager when the MIDlet is about to
* be destroyed (removed from memory). You should take this as an opportunity
* to clear up any resources and save the game. For this example we cancel
* the timer.
* @param unconditional if false you have the option of throwing a
* MIDletStateChangeException to abort the destruction process.
* @throws MIDletStateChangeException
*/
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException
{
timer.cancel();
}
/**
* An example of a TimerTask that adds some text to the form.
*/
class PrintTask extends TimerTask
{
/**
* To implement a task you need to override the run method.
*/
public void run()
{
// output the time the task ran at
form.append("" + scheduledExecutionTime());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -