📄 midletdemo.java~11~
字号:
//MIDletDemo.javapackage ORA.ch3;import java.util.Timer;import java.util.TimerTask;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class MIDletDemo extends MIDlet{ private boolean started = false; private Thread thread; private int timerInterval; private Timer timer; private TimerTask task; public MIDletDemo() { System.out.println("**************Constructor Executed!**************"); String interval = getAppProperty("Timer-Interval"); timerInterval = Integer.parseInt(interval); System.out.println("Timer interval is " + interval); } protected void startApp() throws MIDletStateChangeException { if (!started) { started = true; System.out.println("startApp called for the first time!"); startTimer(); } else { System.out.println("startApp called following pause!"); } synchronized (this) { if (thread == null) { thread = new Thread() { public void run() { System.out.println("Thread running!"); while (thread == this) { try { Thread.sleep(1000); System.out.println("Thread still alive!"); } catch (InterruptedException ex) { } } System.out.println("Thread terminating!"); } }; } }; thread.start(); } protected void pauseApp() { System.out.println("pauseApp called!"); synchronized (this) { if (thread != null) { thread = null; } } } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { System.out.println("destroyApp called - unconditional = " + unconditional); if (thread != null) { Thread bgThread = thread; thread = null; try { bgThread.join(); } catch (InterruptedException ex) { } } stopTimer(); } private void startTimer() { task = new TimerTask() { private boolean isPaused; private int count; public void run() { System.out.println("Timer scheduled!"); if (count++ == 4) { //终止MIDlet try { MIDletDemo.this.destroyApp(true); } catch (MIDletStateChangeException ex) { } MIDletDemo.this.notifyDestroyed(); return; } if (isPaused) { System.out.println(">> Resuming MIDlet"); MIDletDemo.this.resumeRequest(); isPaused = false; } else { System.out.println(">> Pauseing MIDlet"); isPaused = true; MIDletDemo.this.pauseApp(); MIDletDemo.this.notifyPaused(); } //else } //run() }; //new timer = new Timer(); timer.schedule(task,timerInterval,timerInterval); System.out.println("Timer Started!"); } //startTimer(); private void stopTimer() { if(timer != null) { System.out.print("Stopping the timer"); timer.cancel(); } }//stopTimer()}//MIDletDemo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -