J2ME中使用pauseApp控制手機臨時退出JAVA程式

來源:互聯網
上載者:User

Midlet(即JAVA程式)處於活動狀態時,應用程式管理器(手機作業系統)會因為某些情況要求程式暫停 ,比如手機突然來電 或者短訊息 等。應用程式管理器為了節省更多的系統資源 ,會首先調用pauseApp方法釋放一部分Midlet非必須的資源,然後再轉換到暫停狀態。所以一般在pauseApp()方法內應該添加釋放資源的必需代碼。

我在程式中使用狀態參數控制遊戲流程:

  private static final int intGameWaiting = -1;
  private static final int intGameReady = 0;
  private static final int intGamePlaying = 1;
  private static final int intGamePause = 2;
  private static final int intGameWon = 3;
  private static final int intGameEnd = 4;
  private static final int intGameQuit = 5;

  public int intGameStatus = -1;
  public int intGameStatusReserve = -1;

protected void startApp() {

  if(intGameStatus == intGamePause)
  {
   PlayCanvas = new clsPlayCanvas();
   PlayCanvas.setFullScreenMode(true);
   display.setCurrent(PlayCanvas);
   intGameStatus = intGameStatusReserve;
   PlayCanvas.continueGame();
  }else{
   MainCanvas = new clsMainCanvas();
   MainCanvas.setFullScreenMode(true);
   display.setCurrent(MainCanvas);
  }
  }

 protected void pauseApp() {
  //isGameWaiting = true;
  if(intGameStatus!=intGameWaiting)
  {
   PlayCanvas = null;
   intGameStatusReserve = intGameStatus;
   intGameStatus = intGamePause;
  }
 }
這樣就保證了程式在臨時退出後返回繼續遊戲。

如果需要可以再設計一個PAUSE的CANVAS來過渡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.