当前页面: 开发资料首页 → J2ME 专题 → Pass J2ME (1) - MIDP State
摘要: Pass J2ME (1) - MIDP State
对于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
 *
 * 
Title: Midlet Test
Description:
Copyright: Copyright (c) 2005
Company: http://blog.csdn.net/Vincint
  /**
   * 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);
  }
}