L
ets get our hands dirty. In this chapter, we are going to write our first MIDlet. This is the fifth chapter in the 'Java ME Beginners Tutorial' series.

By the time you reach here, you are expected to have gone through the introduction, installed WTK on your work machine and learned about it. We also had a brief look into the Java ME world. In this chapter we will begin coding.

If you remember earlier chapters, it was said that Wireless Tool Kit is not an IDE. There is no facility to edit and indent code. So we have to use a text editor for this purpose. For the purpose of this tutorial, we will use Notepad available in windows (Linux users have a myriad of options: vi, emacs, gedit, kate, kwrite etc etc...).

Step 1


Fire up WTK (Start -> All Programs ->Sun Java(TM) Wireless Toolkit 2.5 for CLDC ->Wireless Toolkit 2.5 or [INSTALLDIR]\bin\ktoolbar.exe). Then select "New Project" from the toolbar.


You will get a dialog box asking for Project Name and Midlet class name, as shown below.


Provide "JavaMEBlog" as the project name and "HelloMIDlet" as the class name and click "Create Project". You will get a settings page. This is the configuration page where you can edit the settings of your project. The default settings will work for us, so click "OK". If all went correct, a folder named JavaMEBlog would have been created in the [INSTALLDIR]\apps\ directory (INSTALLDIR is the place where you installed WTK). This is the main project folder. It contain many sub directories :

  • bin:Will contain the binary files: JAD and JAR after compiling the code. (We will see JAD and JAR files later when we discuss "Packaging")
  • lib:Place the library files(again in JAR format) that the application requires in this directory.
  • res: Place resources like icons, images, audio, video files etc that the application requires during run-time in this directory.
  • src:The most important directory for the application developer, as this directory contains all the source code.

Step 2


Copy-paste the following code into Notepad:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class HelloMIDlet extends MIDlet {
public void startApp() {
System.out.println("Hello World");
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

Now save it as "HelloMIDlet.java". If using Notepad, be careful to make the "save as type" to "All files". Otherwise notepad will name the file as HelloMIDlet.java.txt which is not what we intended. You have to save this file in the [INSTALLDIR]\apps\JavaMEBlog\src directory.

Step 3


Now we can compile the code. Select "Build" from the tool bar. Building is a sequence of two operations: Compiling the code and then preverifying it. If your code was correct you will get the "Build complete" message as shown in the figure.




Step 4


Next step is to test the application. We use the Wireless Tool Kit's emulator for this purpose. Select "Run" from the tool bar.

Our application doesn't do anything useful. All it does is to print "Hello World" in the WTK console and exit (duh!); so there is no point in installing such an application in an actual mobile device since there is no console in mobile devices.

On running the application, the default emulator will be displayed (which will be Default color phone, if you haven't changed any settings). The application name "JavaMEBlog" will be displayed on the screen. Select "Launch". Since out application doesn't have a GUI, you will not notice any difference in the screen. But the string "Hello World" would be printed on the WTK's console.


Understanding the source code



public class HelloMIDlet extends MIDlet {

Every MIDlet should implement the abstract class javax.microedition.midlet.MIDlet. This class contains three abstract methods that we have to implement: startApp(),pauseApp() and destroyApp(). Every MIDlet is invoked and controlled by the Application Management System (AMS) residing in the mobile phones. The above three methods are special as they are never called by the developer(application) but by the AMS. Whenever the user wishes to run the application, the AMS invokes the startApp() method. When the AMS wishes to pause the application (such as during an incoming call) the pauseApp() method is called. Finally, when the AMS wants to terminate the application, the destroyApp() is called.


public void startApp() {
System.out.println("Hello World");
}

The stuff within the startApp() method will be executed when the application is launched by the user. Here, we have given as simple println statement. The System.out.println() will print to the WTK's console area. So it can be used for testing with an emulator only.

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}


We are not doing anything when pauseApp() or destroyApp() is being called. In actual programming, we will be doing some clean up work (freeing some resources) in these methods.

Since we have gone through our first code, now is a good time to discuss the life cycle of a MIDlet application.



There are three states for every MIDlet: Active, Paused and Destroyed. When the MIDlet object is made by calling its constructor, the application is said to be in the Paused state. Then the AMS can move the application to Active state by calling the startApp() method. From the Active state, the AMS can take the application back to Paused state by calling the pauseApp() method. The thing to remember here is that startApp() and pauseApp() can be called multiple times in an application's life time.
When the AMS wants to terminate the application, it calls the destroyApp() method. destroyApp() accepts a boolean parameter. If this parameter is true, the MIDlet has to compulsorily quit, no matter what it is currently doing. However if this is false, the application can survive termination by throwing a MIDletStateChangeException. If any runtime exception occurs within the destroyApp() method, it is ignored and the application is terminated immediately.

Next we will discuss how to package the compiled classes to a package so that other people can easily install our application.






Previous: Java ME Basics Table of Contents Next: Packaging