public class ExceptionTest extends MIDlet implements CommandListener { PRivate Display display; // Reference to Display object private Form frmMain; // A Form private Command cmdExit; // A Command to exit the MIDlet private boolean safeToExit = false; // Is it safe to exit the MIDlet?
public ExceptionTest() { display = Display.getDisplay(this);
cmdExit = new Command("Exit", Command.SCREEN, 1); frmMain = new Form("Test Exception"); frmMain.addCommand(cmdExit); frmMain.setCommandListener(this); }
// Called by application manager to start the MIDlet. public void startApp() { display.setCurrent(frmMain); }
// We are about to be placed in the Paused state public void pauseApp() { }
// We are about to enter the Destroyed state public void destroyApp(boolean unconditional) throws MIDletStateChangeException { System.out.println("Inside destroyApp()");
// If we do not need to unconditionally exit if (unconditional == false) { System.out.println("Requesting not to be shutdown"); throw new MIDletStateChangeException("Please don't shut me down."); } }
// Check to see if the Exit command was selected public void commandAction(Command c, Displayable s) { if (c == cmdExit) { try { // Is it ok to exit? if (safeToExit == false) destroyApp(false); else { destroyApp(true); notifyDestroyed(); } } catch (MIDletStateChangeException excep) { safeToExit = true; // Next time, let's exit System.out.println(excep.getMessage()); System.out.println("Resuming the Active state"); } } } }