midletdemo.java~8~

来自「J2ME高级用户界面的学习代码」· JAVA~8~ 代码 · 共 128 行

JAVA~8~
128
字号
//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");          ExampleMIDlet.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 + =
减小字号Ctrl + -
显示快捷键?