J2ME road——J2ME实现Lifecycle

来源:互联网 发布:ck内裤价格知乎 编辑:程序博客网 时间:2024/05/16 14:40

直接贴代码。。切入正题!

package src;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class LifecycleTest extends MIDlet implements CommandListener
{
 private Form form;
 private Command quit;
 private boolean forceExit =false;
 
 public LifecycleTest()
 {
  System.out.println("Constructor called.");
  
  form =new Form("Life ,Jason.");
  form.append("But not as we know it.");
  form.setCommandListener(this);
  
  quit = new Command("Quit", Command.SCREEN, 1);
  form.addCommand(quit);
 }

 protected void destroyApp(boolean b) throws MIDletStateChangeException
 {
  
  System.out.println("destroyApp("+ b+ ") called.");
  if(!b)
  {
   forceExit = true;
  }
 }

 protected void pauseApp()
 {
  
  System.out.println("pauseApp() called.");
 }

 protected void startApp() throws MIDletStateChangeException
 {
  
  System.out.println("startApp() called.");
  Display.getDisplay(this).setCurrent(form);
  
 }

 public void commandAction(Command c, Displayable d)
 {
  System.out.println("commandAction(" +c +","+d +") called.");
  
  
   try
   {
    if(c == quit)
    {
     destroyApp(forceExit);
     notifyDestroyed(); 
    }
   }
   
   catch (MIDletStateChangeException e)
   {
    System.out.println(e +"caught.");
   }
 }

}

原创粉丝点击