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來過渡。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=532165