Blog Post: J2ME Tutorials
J2ME Tutorials: Part 1- J2ME Basics (MIDlets)
1311884252midlet-lifecycle1.jpg
Author:Sanjeev Maharjan

Tutorial Prerequisites:

J2ME Applications are applications basically for mobile devices which include mobile phones, PDAs and mobile PCs. In this and coming tutorials we would be dealing with the creation of J2ME application for the general mobile phones with small display, limited memory and limited network access capabilities. Application for devices with such limited capabilities would be developed as MIDlet Applicaion.

Basics of MIDlet:
To create a MIDlet application we need to import two packages:
1. javax.microedition.midlet.*
This package defines the MIDlet class and MIDletStateException class, and is the most important package for any MIDlet application.

2. javax.microedition.lcdui.*;
This package constitutes of all UI components, Event Handling Components, Graphical Components and many more which are essential for mobile applications.

For detail J2ME API documentation, click here

Any MIDlet application must extend MIDlet class. Class which extends the MIDlet would be the entering point for your J2ME application. So, lets see what MIDlet class has to offer for us:

  • MIDlet is an abstract class which provides the methods to control the different states of an MIDlet application. Methods of MIDlet include:
    • int checkPermission(String permission): Get the status of the specified permission.
    • protected abstract void destroyApp(Boolean unconditional):Signals the MIDlet to terminate and enter the Destroyed state.
    • String getAppProperty(String key):Provides a MIDlet with a mechanism to retrieve named properties from the application management software.
    • void notifyDestroyed():Used by an MIDlet to notify the application management software that it has entered into the Destroyed state.
    • void notifyPaused():Notifies the application management software that the MIDlet does not want to be active and has entered the Paused state.
    • protected abstract void pauseApp():Signals the MIDlet to enter the Paused state.
    • booloean platformRequest(String url):Requests that the device handle (for example, display or install) the indicated URL.
    • void resumeRequest() :Provides a MIDlet with a mechanism to indicate that it is interested in entering the Active state.
    • protected abstract void startApp():Signals the MIDlet that it has entered the Active state.
  • MIDlet when extended requires three methods to be overridden and that would be
    • void startApp();
    • void pauseApp();
    • void destroyApp(boolean unconditional);
  • When MIDlet class is extended we have make sure that there is one no argument constructor and if you donot want to define that, you have to make sure that there are no other constructors so that compiler will define one no argument constructor by default.

 

 

Creating Our First MIDlet:

//This is the MIDP version of the familiar MyFirstApp program.
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
/**
Creates the " MyFirstApp " program in J2ME MIDP. Note that the class must be public so that the device application management software can instantiate it.
*/
public class MyFirstApp extends MIDlet
{
// The Displayable. This component is displayed on the screen.
private Form form;
// The Display. This object manages all Displayable components.
private Display display;
//more on UI components will be discussed in next tutorial
// A public no-arg constructor is necessary, even though the system calls startApp()! The AMS calls the
// class's no-arg constructor to instantiate the class. Either create a public no-arg constructor, or declare
// no constructors, and let the compiler create a public no-arg constructor.

public MyFirstApp()
{
super(); // calling parameterless constructor of MIDlet class
}
//startApp will be called once constructor task is complete

//required resources are allocated in either constructor or startApp
public void startApp()
{
// Create a Displayable widget.
form = new Form("My First App"); //setting title of the form
// Add a string to the form.
String msg = "My first MIDlet Application";
form.append(msg);
// This app simply displays the single form created above.
display = Display.getDisplay(this); // gets the display component of MIDlet class
display.setCurrent(form); //sets the above created form as the current display
} //end of StartApp

public void destroyApp(boolean destroy)
{
form = null;
notifyDestroyed();
} //end of destroyApp

//Monitor on shared resources are released when pauseApp is called
public void pauseApp()
{
} //end of pauseApp

}// end of MyFirstApp

End of First Tutorial... We will be discussing about UI components and event handling components in next tutorial... Till then keep coding...

 

COMMENTS

No Comments Yet

Post a comment
* marked are required fields
*Name:
*Email Address:
*City:
*Comments
In order to help us prevent automated submissions, please type the number shown in below picture
*Validation no.
captcha
My Latest Tweets
Download Archive Here:
Twitter LinkedIn Blogger Behance Portfolio Facebook