⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lifecycletest.java

📁 大量j2me源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * An MIDlet which demonstrates the lifecycle of a MIDlet
 * @author Martin J. Wells
 */
public class LifecycleTest extends javax.microedition.midlet.MIDlet
        implements CommandListener
{
   private Form form;
   private Command quit;
   private boolean forceExit = false;

   /**
    * Constructor for the MIDlet which creates a simple Form, adds some text and
    * an exit command. When called this method will also write a message to the
    * console. This is used to demonstrate the sequence of events in a MIDlet's
    * lifecycle.
    */
   public LifecycleTest()
   {
      System.out.println("Constructor called.");

      form = new Form("Life, Jim.");
      form.append("But not as we know it.");
      form.setCommandListener(this);

      // Create and add two commands to change the MIDlet state
      quit = new Command("Quit", Command.SCREEN, 1);
      form.addCommand(quit);
   }

   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this example it acquires the current Display object
    * and uses it to set the Form object created in the MIDlet constructor as
    * the active Screen to display. Also displays a message on the console when
    * called to demonstrate when this method is called in the MIDlet lifecycle.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      System.out.println("startApp() called.");
      Display.getDisplay(this).setCurrent(form);
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we output a message on the console
    * to indicate when this method is called in the MIDlet's lifecycle.
    */
   protected void pauseApp()
   {
      System.out.println("pauseApp() called.");
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we output a
    * message on the console to indicate when this method is called in the
    * MIDlet lifecycle.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
      System.out.println("destroyApp(" + unconditional + ") called.");

      if (!unconditional)
      {
         // we go through once using unconditional, next time it's forced.
         forceExit = true;
      }
   }

   /**
    * The CommandListener interface method called when the user executes a
    * Command, in this case it can only be the quit command we created in the
    * consutructor and added to the Form. We also output a console message when
    * this method is called.
    * @param command the command that was triggered
    * @param displayable the displayable on which the event occurred
    */
   public void commandAction(Command command, Displayable displayable)
   {
      System.out.println("commandAction(" + command + ", " + displayable +
                         ") called.");
      try
      {
         if (command == quit)
         {
            destroyApp(forceExit);
            notifyDestroyed();
         }
      }

      catch (MIDletStateChangeException me)
      {
         System.out.println(me + " caught.");
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -