Hello World in J2ME

xiaoxiao2021-03-06  77

When I first studied Java, I usually wrote the following HelloWorld program. Today I am going to tell the HelloWorld in J2ME. Whether you are a J2ME development expert or a newbie, you should read this article, I think it will help you!

In the javax.microedition.midlet package, a very important class MIDLET is defined. All J2ME applications must extend this class. Only this can make the application management software (Application Management Software manages MIDlet, including download, installation, and delete . While being managed by AMS, MIDLET can communicate with the application management software communication to notify the application management software's own state, usually through method notifyDestroyed () and notifypaused (). Finally, MIDlets can also be read through GetAppProperty (String Name) to read the attribute value defined in the JAD file.

MIDlet has three states, which are Pause, Active, and Destroyed. When starting a MIDlet, the application management software first creates a MIDlet instance and makes him in the PAUSE state. When the startApp () method is called, the MIDlet enters the Active status, that is, the operating state we usually said. Calling DestroyApp (Boolean Unconditional) or PauseApp () method in Active Status enables MIDlet to enter the Destroyed or PAUSE state. It is worth mentioning that the destroyApp (Boolean Unconditional) method, many developers don't understand the unconditional parameters, in fact, when the destroyApp () method is called, the AMS notifies the MIDlet to enter the Destroyed state. The MIDlet in the DESTROYED state must release all resources and save the data. If UNCONDITIONAL is false, the MIDlet can throw MIDletStateChangeException after receiving the notification, if set to TRUE, you must go to the DESTROYED state immediately.

Here is a HelloWorld program I have written according to the above problem, and he is complicated than HelloWorld than J2SE. Novice can take a closer look. Package com.j2medev.mingjava;

Import javax.microedition.midlet.mIdlet; import javax.microedition.midlet.midletStateChangeException; import javax.microedition.lcdui. *;

public class HelloWorld extends MIDlet implements CommandListener {private Display display; private Form mainForm; private StringItem stringItem; private Command exitCommand = new Command ( "Exit", Command.EXIT, 1);

Public Static Final String Web_site = "Web_site";

Protected void startapp () throws midletStateChangeException {

InitmIDlet (); Display.SetCurrent (Mainform);

}

private void initMIDlet () {display = Display.getDisplay (this); mainForm = new Form ( "Hello World"); stringItem = new StringItem (null, null); String text = getAppProperty (WEB_SITE); stringItem.setText (text) Mainform.Append (StringItem); mainform.addcommand (exitcommand); mainform.setcommandListener (this);} protected void pauseApp () {

}

Protected Void DestroyApp (Boolean Arg0) Throws MidletStateChangeException {System.Out.Println ("EXIT The Application");}

public void commandAction (Command cmd, Displayable display) {if (cmd == exitCommand) {try {destroyApp (false); notifyDestroyed ();} catch (MIDletStateChangeException e) {e.printStackTrace ();}}}

}

The content of the helloworldmidlet.jad file is as follows, if you are not very clear, please refer to the property problem in MIDP. Especially when you are installing MIDlet to your phone. MIDlet-Jar-Size: 2128MIDlet-1: HelloWorld, / icon.png, com.j2medev.mingjava.HelloWorldMIDlet-Jar-URL: HelloWorldMIDlet.jarMicroEdition-Configuration: CLDC-1.0MIDlet-Version: 1.0.0MIDlet-Name: HelloWorldMIDletMIDlet- Vendor: www.j2medev.commicroedition-profile: MIDP-1.0Web_site: www.j2medev.com

转载请注明原文地址:https://www.9cbs.com/read-121782.html

New Post(0)