MOBILE

The Hello-World Midlet

9/3/2010 8:02:45 PM

The following list gives a line-by-line analysis of the program HelloMIDlet.java in Figure 11.4 (ref.: Java Me Programming Steps).

  • import javax.microedition.lcdui.*;

The package javax.microedition.lcdui provides a set of features for implementing user interfaces for MIDP applications. The class hierarchy is

- java.lang.Object
- javax.microedition.lcdui.AlertType
- javax.microedition.lcdui.Command
- javax.microedition.lcdui.Display
- javax.microedition.lcdui.Displayable
- javax.microedition.lcdui.Canvas
- javax.microedition.lcdui.Screen
- javax.microedition.lcdui.Alert
- javax.microedition.lcdui.Form
- javax.microedition.lcdui.List
(implements javax.microedition.lcdui.Choice)(implements javax.microedition.lcdui.Choice)
- javax.microedition.lcdui.TextBox - javax.microedition.lcdui.Font - javax.microedition.lcdui.Graphics - javax.microedition.lcdui.Image - javax.microedition.lcdui.Item - javax.microedition.lcdui.ChoiceGroup - javax.microedition.lcdui.DateField - javax.microedition.lcdui.Gauge - javax.microedition.lcdui.ImageItem - javax.microedition.lcdui.StringItem - javax.microedition.lcdui.TextField - javax.microedition.lcdui.Ticker

The interface hierarchy is

- javax.microedition.lcdui.Choice
- javax.microedition.lcdui.CommandListener
- javax.microedition.lcdui.ItemStateListener

  • import javax.microedition.midlet.*;

The package javax.microedition.midlet defines MIDP applications and the interactions between the application and the environment in which the application runs. The class hierarchy is

- java.lang.Object
- javax.microedition.midlet.MIDlet
- java.lang.Throwable
- java.lang.Exception
- javax.microedition.midlet.MIDletStateChangeException

  • public class HelloMIDlet extends MIDlet implements CommandListener {

The javax.microedition.midlet.MIDlet is an MIDP application. The application must extend this class to allow the application management software to control the MIDlet and to be able to retrieve properties from the application descriptor and notify and request state changes. The interface javax.microedition.lcdui.CommandListener is used by applications that need to receive high-level events from the implementation. An application will provide an implementation of a Listener and will then provide an instance of it on a Screen in order to receive high-level events on that screen.

  • private Display display;

The javax.microedition.lcdui.Display represents the manager of the display and input devices of the system. It includes methods for retrieving the properties of the device and for requesting the objects to be displayed.

  • private TextBox tbxMain;

The javax.microedition.lcdui.TextBox is a Screen that allows the user to enter and edit text.

  • private Command cmdExit;

The javax.microedition.lcdui.Command is a construct that encapsulates the semantic information of an action.

  • public HelloMIDlet( ) {

The purpose of a constructor is to initialize the instance variables of a newly instantiated object. The J2ME Application Manager (JAM) is responsible for installing, running, and removing MIDlets from the handheld devices. When a user chooses to run a MIDlet, it is the JAM that creates an instance of the MIDlet class and runs methods on it. The sequence of methods that will be called in the MIDlet subclass is defined by the MIDlet life cycle, which is shown in Figure 11.6.

The application manager calls methods in the MIDlet to signify changes from one state to another. startApp( ), pauseApp( ), and destroyApp( ) are the three life-cycle methods that every MIDlet must define.

Figure 11.6. The MIDlet life cycle

A MIDlet goes through the following states:

  1. When the MIDlet is about to be run, an instance is created. The MIDlet's constructor is run, and the MIDlet is in the Paused state.

  2. Next, the MIDlet enters the Active state after the application manager calls startApp( ).

  3. While the MIDlet is Active, the JAM can suspend its execution by calling pauseApp( ), which puts the MIDlet back in the Paused state.

  4. The application manager can terminate the execution of the MIDlet by calling destroyApp( ), at which point the MIDlet is Destroyed and patiently awaits garbage collection.

  • display = Display.getDisplay( this );

The method public static Display javax.microedition.lcdui.Display.getDisplay( MIDlet m ) gets the Display object that is unique to this MIDlet.

  • cmdExit = new Command( "Exit", Command.SCREEN, 1 );

The constructor public javax.microedition.lcdui.Command(String label, int commandType, int priority) creates a new command object with the following parameters:

  • label—The label string is what the application requests be shown to the user to represent this command.

  • commandType—The command types include BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, or STOP.public static final int SCREENspecifies an application-defined command that pertains to the current screen

  • priority—The application uses the value to describe the importance of this command relative to other commands on the same screen.

  • tbxMain = new TextBox( "HelloMIDlet","Hello, World!",50, 0);

The constructor public javax.microedition.lcdui.TextBox(String title, String text, int maxSize, int constraints) creates a new TextBox object with the following parameters:

  • title—The title text is to be shown with the display.

  • text—The initial contents of the text editing area. If the text parameter is null, the TextBox is created empty.

  • maxSize—The maximum capacity in characters, which must be greater than zero.

  • constraints—The different constraints allow the application to request that the user's input be restricted in a variety of ways. Constant 0 is assigned to public static final int ANY, which allows users to enter any text. Other constraints can be found from the Field Summary of input constraints.

  • tbxMain.addCommand( cmdExit );

The method public void javax.microedition.lcdui.Displayable.addCommand( Command cmd ) adds a command to the Displayable. The implementation may choose, for example, to add the command to any of the available soft buttons or place it in a menu.

  • tbxMain.setCommandListener(this);

The method public void javax.microedition.lcdui.Displayable.setCommandListener( CommandListener l ) sets a listener for Commands to this Displayable, replacing any previous CommandListener.

  • public void startApp( ) {

The MIDlet enters the Active state after the application manager calls startApp( ) via clicking on the button Launch.

  • display.setCurrent( tbxMain );

When a MIDlet application is first started, there is no current Displayable object. It is the responsibility of the application to ensure that a Displayable is visible and can interact with the user at all times. Thus, the application should always call setCurrent( ) as part of its initialization. The method public void javax.microedition.lcdui.Display.setCurrent (Displayable next-Displayable) requests that a different Displayable object be made visible on the display.

  • public void pauseApp( ) { }

    public void destroyApp( boolean unconditional ) { }

These two methods usually have empty bodies.

  • public void commandAction( Command c, Displayable s ) {

This method implements the method public void javax.microedition.lcdui.CommandListener.commandAction(Command c, Displayable d), which indicates that a command event has occurred on Displayable d.

  • if ( c == cmdExit ) {

This checks to see if the Exit command was selected.

  • destroyApp( false );

A MIDlet can destroy itself by calling the method destroyApp( ), which is empty in this case.

  • notifyDestroyed( );

The method public final void javax.microedition.midlet.MIDlet.notifyDestroyed( ) notifies the application management software that it has entered into the DestroyeddestroyApp method, and all resources held by the MIDlet will be considered eligible for reclamation. The MIDlet must have performed the same operations (clean up, releasing of resources, etc.) as it would have if the MIDlet.destroyApp( ) had been called. state. The JAM software will not call the MIDlet's

Other  
 
Top 10
Windows Server 2003 : Maintaining, Monitoring, and Troubleshooting Printers
Windows Server 2003 : Advanced Printer Configuration and Management
Windows Server 2003 : Installing and Configuring Printers
Windows 7 : Sharing Resources on a Network - Turn on Sharing and Discovery
Windows 7 : Sharing Resources on a Network - Methods for Sharing in Windows 7
HP Network Node Manager 9 : Before we Manage with NNMi (part 5) - Installing software
HP Network Node Manager 9 : Before we Manage with NNMi (part 4) - Server sizing considerations, Licensing policy
HP Network Node Manager 9 : Before we Manage with NNMi (part 3) - Understanding Smart Plug-ins - iSPI Network Engineering toolset, iSPI IP Telephony, iSPI for MPLS, iSPI multicast
HP Network Node Manager 9 : Before we Manage with NNMi (part 2) - Understanding Smart Plug-ins - iSPI Performance for Metrics
HP Network Node Manager 9 : Before we Manage with NNMi (part 1) - What can HP SW NNMi do for us?
Most View
WD's My Passport (2TB) - Never leave home without it
The best of the web (Part 2) - Evernote & ChallengePost
The Effect Of IOS And Facebook On Shutterbugs (Part 1)
Sharepoint 2007: Upload a File Using the Explorer View
Windows Vista : Performing Local PC Administration (part 1) - Working with workstation administration tools
Windows XP : Verifying Digitally Signed Files, Reviewing Event Viewer Logs, Setting Up a 10-Step Maintenance Schedule
Personalizing Windows 7 (part 2) - Choosing Your Desktop Background
.NET Enterprise Services Technologies : Application Blocks and Software Factories
Fujifilm FinePix F800EXR Super-Zoom Compact With WiFi Connection
Windows Azure : Storing static reference data with dynamic data
Lenovo ThinkPad X220
Windows 7 : Recovering After a Crash or Other Problem (part 3)
Microsoft Wedge And Sculpt Kits For Windows 8
Windows 7 : Using Windows Defender (part 1) - Configuring Windows Defender
Microsoft XNA Game Studio 3.0 : Getting the Date and Time
Windows Azure : Messaging with the queue - Working with basic queue operations
Audioengine A5+ Powered Speakers
Registering CLR Assemblies for Use with SQL Server
iPhone Application Developmen : Using the View-Based Application Template (part 3)
Use the new BBC website (Part 1)