The Making of a MIDlet

Now that you know what Java ME is, let’s talk about the NetBeans IDE, and create our first mobile project. We’ll also discuss the required methods of a MIDlet class.

If you plan on developing Java ME applications, more than likely you will want to use an IDE (Integrated Development Environment). Today’s IDEs are much more than just text editors; they provide integrated support for compiling, debugging, device emulation, and GUI building. In other words, they are a big time-saver. Luckily, there exists an excellent IDE for Java ME development called NetBeans. NetBeans is robust, well supported, cross-platform, and let’s not forget that it’s free.

In order to develop Java ME applications using NetBeans, you will need to download and run two installers: one for NetBeans IDE, and another for NetBeans Mobility Pack. The IDE should be installed first. Both of these installers can be downloaded from the official NetBeans website. In addition, NetBeans requires Java SE 1.4.2 or later to be installed. If you don’t have it already, an installer of Java SE bundled with NetBeans can be downloaded from the Sun website.

New Project in NetBeans Once you have the IDE and Mobility Pack installed, you can start your first project. Start NetBeans IDE, and from the “File” menu, choose “New Project…”

Mobile Application in NetBeans In the “New Project” dialog that appears, select the category “Mobile”, and the project “Mobile Application,” and click “Next.”

Project Name and Location in NetBeans In the next step, choose a project name and location for your project files. Deselect the “Create Hello MIDlet” option, and click “Next.”

Emulator Platform in NetBeans
Lastly, for emulator platform, choose “J2ME Wireless Toolkit x.x,” and “Default Color Phone” as the device. Here is where you can select between CLDC and MIDP environments. For now, you can leave these with the defaults; they can always be changed later in the project properties. Click “Finish.”

Congratulations, you’ve created your first NetBeans project. Now let’s look at adding a basic MIDlet class to your project. NetBeans has a convenient option for generating an empty MIDlet class.

Creating a MIDlet Class in NetBeans First, right-click on your project in the “Projects” tree, and select “New” > “MIDlet…” from the context menu

Naming a MIDlet Class in NetBeans Next, enter a name for your MIDlet class. The name must be a valid Java identifier, and it should be consistent with Java naming conventions for classes. You may also enter a package name for your class, if you like. If you do not enter a package name, the class will be created in the root package. Once you are finished, click “Next.”

Now your project should have a Java source code file that looks like the one below. I’ve removed the generated comments for brevity.

DemoMidlet.java


package com.dbarnes.demomidlet;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class DemoMidlet extends MIDlet {

public void startApp() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

Notice that your class already has three methods. These are the three abstract methods in the base MIDlet class that every application must override. These methods are called by the application manager to signal state changes.

  • startApp() is called when the application is about to become active, either immediately after it has been loaded, or after being in the paused state. As a result, startApp() can be called multiple times on the same MIDlet. This is generally where the application will initialize its state, set up the display for the first screen, and load required resources. This method should also handle any behavior required for continuing the application from the paused state, such as restarting of threads.
  • pauseApp() is called when the application is about to be paused. Here you’ll want to signal your threads to stop, so that you aren’t running down the battery while the application is paused. You’ll also want to release any shared resources to avoid deadlocks with other applications that might run while your application is paused.
  • destroyApp() is called when the application is about to terminate. Here you should signal your threads to end, release any shared resources, and commit any data that needs to be persisted.

Bookmark this Post

Leave a Reply