Methods Summary |
---|
protected void | destroyApp(boolean unconditional)
// Called to destroy the MIDlet.
System.out.println("destroyApp called - unconditional = "
+ unconditional);
if (thread != null) {
Thread bgThread = thread;
thread = null; // Signal thread to die
try {
bgThread.join();
} catch (InterruptedException ex) {
}
}
stopTimer();
|
protected void | pauseApp()
// Called from the timer task to
// do whatever is necessary to
// pause the MIDlet.
// Tell the background thread to stop.
System.out.println("pauseApp called.");
synchronized (this) {
if (thread != null) {
thread = null;
}
}
|
protected void | startApp()
if (!started) {
// First invocation. Create and start
// a timer.
started = true;
System.out.println("startApp called for the first time");
startTimer();
} else {
// Resumed after pausing.
System.out.println("startApp called following pause");
}
// In all cases, start a background thread.
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 active");
} catch (InterruptedException ex) {
}
}
System.out.println("Thread terminating");
}
};
}
}
thread.start();
|
private void | startTimer()
// Create a task to be run
task = new TimerTask() {
private boolean isPaused;
private int count;
public void run() {
// Pause or resume the MIDlet.
System.out.println("Timer scheduled");
if (count++ == 4) {
// Terminate the MIDlet
try {
ExampleMIDlet.this.destroyApp(true);
} catch (MIDletStateChangeException ex) {
// Ignore pleas for mercy!
}
ExampleMIDlet.this.notifyDestroyed();
return;
}
if (isPaused) {
System.out.println(">> Resuming MIDlet");
ExampleMIDlet.this.resumeRequest();
isPaused = false;
} else {
System.out.println(">> Pausing MIDlet");
isPaused = true;
ExampleMIDlet.this.pauseApp();
ExampleMIDlet.this.notifyPaused();
}
}
};
// Create a timer and schedule it to run
timer = new Timer();
timer.schedule(task, timerInterval, timerInterval);
System.out.println("Timer started.");
|
private void | stopTimer()
if (timer != null) {
System.out.println("Stopping the timer");
timer.cancel();
}
|