對於Midlet狀態,有一個叫application management software的會專門負責管理。你所有要關心的,就是怎樣和這個AM打交道。
pauseApp()
destroyApp()
startApp()
重寫這三個方法。AM將會在相應的時間調用。
而
notifyDestroyed()
notifyPaused()
resumeRequest()
則是讓你有機會去通知AM需要做什麼。
下面是一個簡單的例子,可以根據自己擴充測試一下。有一點比較奇怪的就是,理論上說,在Midlet處於Active狀態的時候,如果有電話進入,AM會調用pauseApp()。但是實際測試時,沒有發現AM調用這個方法。
package net.csdn.blog.vincint;
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
/**
* This is a simple test to help me understanding the Midlet state
*
* <p>Title: Midlet Test</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: http://blog.csdn.Net/Vincint</p>
* @author Vincint
* @version 1.0
*/
public class MidletState
extends MIDlet
implements
CommandListener {
protected Form form;
protected Command exitCmd;
protected Command debugCmd;
protected Display display;
protected String debug;
public MidletState() {
addDebugInfo("In MidletState()");
display = Display.getDisplay(this);
form = new Form("Hello world.");
form.append("What a new world!");
exitCmd = new Command("Exit", Command.EXIT, 1);
debugCmd = new Command("Debug", Command.SCREEN, 2);
form.addCommand(exitCmd);
form.addCommand(debugCmd);
form.setCommandListener(this);
}
/**
* startApp
*/
protected void startApp() {
addDebugInfo("In startApp()");
display.setCurrent(form);
}
/**
* pauseApp
*/
protected void pauseApp() {
addDebugInfo("In pauseApp()");
}
/**
* destroyApp
*
* @param boolean0 boolean
*/
protected void destroyApp(boolean boolean0) {
addDebugInfo("In destroyApp(" + boolean0 + ")");
}
/**
* commandAction
*
* @param command Command
* @param displayable Displayable
*/
public void commandAction(Command command, Displayable displayable) {
addDebugInfo("In commandAction(...)");
if (command == exitCmd) {
destroyApp(true);
notifyDestroyed();
}
else if (command == debugCmd) {
notifyUser(debug, form);
}
}
protected String addDebugInfo(String debugMsg) {
if (debug == null) {
debug = new String();
}
debug = debug.concat(debugMsg + "\n");
return debugMsg;
}
protected void notifyUser(String message, Displayable screen) {
Alert alert = new Alert("Info", message, null, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, screen);
}
}