I have generally written the following helloworld program when I was a beginner in Java. Today I am going to explain in detail the helloworld program in Java. I think it will be helpful for you whether you are a beginner or a beginner in the j2_based development field!
In javax. microedition. the MIDlet package defines a very important class, MIDlet, which must be extended by all j2s' applications. Only in this way can the application management software manage the MIDlet, including downloading, installing, and deleting. While being managed by AMS, The MIDlet can communicate with the application management software to notify the application management software of its status changes, usually through notifydestroyed () and yypaused. At last, the MIDlet can use getappproperty (string name) to read the attribute values defined in the JAD file.
The MIDlet has three states: pause, active, and destroyed. When starting a MIDlet, the application management software first creates a MIDlet instance and puts it in the pause state. When the Startapp () method is called, The MIDlet enters the active state, this is what we usually call the running status. In the active state, call the destroyapp (Boolean unconditional) or pauseapp () method to enable the MIDlet to enter the destroyed or pause state. It is worth mentioning that the destroyapp (Boolean unconditional) method, many developers do not quite understand the unconditional parameter. In fact, when the destroyapp () method is called, AMS notifies the MIDlet to enter the destroyed state. In the destroyed state, all resources must be released and data is saved. If unconditional is false, the MIDlet can throw the midletstatechangeexception after receiving the notification and keep it in the current state. If it is set to true, it must immediately enter the destroyed state.
The following is a helloworld program in j2s compiled based on the above problems. It is a little more complex than helloworld in j2se. You 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;
Private form mainform;
Private 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 );
Yydestroyed ();
} Catch (midletstatechangeexception E)
{
E. printstacktrace ();
}
}
}
}
The content of the helloworldmidlet. JAD file is as follows. If you are not clear about the JAD file, refer to the attributes in MIDP. Especially when you install the MIDlet on your phone.
MIDlet-jar-size: 2128
MIDlet-1: helloworld,/icon.png, Com. j2medev. mingjava. helloworld
MIDlet-jar-URL: helloworldmidlet. Jar
Microedition-configuration: CLDC-1.0
MIDlet-version: 1.0.0
MIDlet-Name: helloworldmidlet
MIDlet-vendor: www.j2medev.com
Microedition-profile: MIDP-1.0
Web_site: www.j2medev.com