当前页面: 开发资料首页 → J2ME 专题 → 深入MIDIetStateChangeException
摘要: 深入MIDIetStateChangeException
/*----------------------------------------------------
* a look inside MIDletStateChangeException and the
* destroyApp() method
*
* www.CoreJ2ME.com
*---------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
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");
}
}
}
}
bruceyuki,JAVA C#技术爱好者,现就读于新西兰奥克兰大学,正参与大学的一个AI项目,可以点击http://www.matrix.org.cn/user_view.asp?username=bruceyuki查看他的个人信息