1、在处理过程中,如果按照应用程序管理器的要求发生中断,会引起不可挽回的损失。
2、可能通过抛出一个MIDletStateChangeException异常收回MIDlet的控制权。临时应用程序管理器发出的请求。当MIDLET正在进行不应被应用程序管理器中断的处理时,就加入MIDletStateChangeException的抛出程序段。
3、下例采用从destroyApp()方法中抛出MIDletStateChangeException。本例第一次按退出时,假设系统正在处理中,会抛出MIDletStateChangeException阻止系统退出。
1)NETBEANS的代码如下:
package hello;
import javax.microedition.apdu.APDUConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet implementsCommandListener,ItemStateListener {
privateboolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc="Generated Fields">
privateCommand exitCommand;
private Formform;
privateChoiceGroup weather_CG;
//</editor-fold>
private StringItem warnmsg_SI;
private boolean blissafeexit;//是否可以安全退出
publicHelloMIDlet() {
}
//<editor-fold defaultstate="collapsed" desc="Generated Methods">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: initialize">
private voidinitialize(){
// write pre-initialize user code here
blissafeexit=false;
// write post-initialize user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: startMIDlet">
public voidstartMIDlet(){
// write pre-action user code here
switchDisplayable(null,getForm());
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: resumeMIDlet">
public voidresumeMIDlet(){
// write pre-action user code here
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: switchDisplayable">
public voidswitchDisplayable(Alert alert, Displayable nextDisplayable){
// write pre-switch user code here
Display display =getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: commandAction for Displayables">
public voidcommandAction(Command command, Displayable displayable){
// write pre-action user code here
if (displayable == form){
if (command == exitCommand){
// write pre-action user codehere
exitMIDlet();
// write post-action user code here
}
}
// write post-action user code here
}
//</editor-fold>
//重写ItemStateChanged方法
public voiditemStateChanged(Item item) {
// write pre-action user code here
if (item == weather_CG) {
form.setTitle("你选择了"+weather_CG.getString(weather_CG.getSelectedIndex())+"天");
// write post-action user codehere
}
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: exitCommand">
publicCommand getExitCommand() {
if (exitCommand == null){
// write pre-init user code here
exitCommand = new Command("/u9000/u51FA", Command.EXIT,0);
// write post-init user code here
}
return exitCommand;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: form">
public FormgetForm() {
if (form == null){
// write pre-init user code here
form = new Form("Welcome", new Item[] { getWeather_CG()});
form.addCommand(getExitCommand());
form.setCommandListener(this);
//增加初始天气选择情况显示
form.setTitle("你选择了晴天");
//增加ITEM的监听器
form.setItemStateListener(this); // write post-init user code here
}
return form;
}
//</editor-fold>
publicStringItem getStringItem(){
warnmsg_SI=new StringItem("系统忙","请再试一次");
return warnmsg_SI;
}
//<editor-fold defaultstate="collapsed" desc="Generated Getter: weather_CG">
publicChoiceGroup getWeather_CG() {
if (weather_CG == null){
// write pre-init user code here
weather_CG = new ChoiceGroup("/u5929/u6C14/u7C7B/u578B",Choice.EXCLUSIVE);
weather_CG.setLayout(ImageItem.LAYOUT_DEFAULT);
weather_CG.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
//选项框项的代码
weather_CG.append("晴",null);
weather_CG.append("阴",null);
weather_CG.append("雨",null);
weather_CG.append("雪",null);
weather_CG.setSelectedIndex(0, true);
// write post-init user code here
}
return weather_CG;
}
//</editor-fold>
publicDisplay getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
//检查是否为强行退出
try{
if(!blissafeexit){
form.append(getStringItem());
destroyApp(false); //为强行退出,destroyApp会抛出异常,临时丢弃应用程序管理器发出的请求。
}
else{
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
}
catch(MIDletStateChangeException exception){
blissafeexit=true;
}
}
public voidstartApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public voidpauseApp() {
midletPaused = true;
}
public void destroyApp(booleanunconditional) throws MIDletStateChangeException
{
if(!unconditional){
throw new MIDletStateChangeException();
}
}
}
2)效果图