国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

深入MIDIet State Change Exception

2019-11-18 12:29:43
字體:
來源:轉載
供稿:網友

  當MIDIet程序運行時出現錯誤的時候,應用程序治理器(application manager)會發出這個異常(exception). 在MIDIet類中也有兩個方法可以發出這個異常(exception): destroyApp()和startApp().
  
  下面是一個捕捉和發送例外的例子。這個簡單的MIDIet顯示一個FORM和一個關閉程序的按鈕(顯示EXIT).
  
  創建這個例子的時候,我們會有這個問題:當按下關閉按鈕或者應用程序治理器(application manager)要關閉這個MIDIet的時候,假如MIDIet正在下載程序,會發生什么情況?我們可以拋出異常(exception)而不是馬上關閉程序。也就是說,下載請求不應在此時被關閉。應用程序治理器會在以后重試。
  
  這個例子如何工作:當使用者第一次按下EXIT按鈕,我們調用destroyApp(false)方法。這里傳入參數false告訴方法這個請求不是無條件的,而且我們將會拋出MIDIetStateException異常.這個異常(exception)會在commandAction()里被捕捉到,同時會設置一個標志,這個標志意味著我們將可以關閉程序. 這樣,當我們再一次按下EXIT按鈕的時候。MIDIet將要繼續運行直到EXIT按鈕第二次被按下。
  
  注重:以下例子基于MIDP和CLDC 1.0.3.
  
  1. java源代碼:
  /*----------------------------------------------------
  * 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");
  }
  }
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 温州市| 绥化市| 贵州省| 芜湖市| 绥棱县| 海晏县| 株洲县| 长海县| 宁乡县| 昌邑市| 东港市| 资兴市| 东宁县| 宝应县| 界首市| 扶风县| 寻乌县| 金昌市| 武穴市| 武强县| 汉川市| 巴林右旗| 武清区| 杨浦区| 旺苍县| 华池县| 翁牛特旗| 宁城县| 天祝| 佛冈县| 梁平县| 南雄市| 福鼎市| 阿拉善盟| 舞钢市| 太保市| 桂平市| 疏勒县| 柯坪县| 五大连池市| 睢宁县|