April 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Java 2 Micro Edition and the Mobile Information Device Profile

  • November 15, 2000
  • By David Fox
  • Send Email »
  • More Articles »

Who wouldn't love to be able to write cool wireless networking programs and then have them work on pretty much every cell phone or handheld device? Java 2 Micro Edition (J2ME) is touted as the way to achieve this — write once and run anywhere, easy as pie. But how useful is J2ME, really? Currently the reference implementation for J2ME is the Palm Pilot. You can read an intro article about Java Palm Pilot programming here (Java 2 Micro Edition and the Kilobyte Virtual Machine), or read about how to develop a simple J2ME Palm application here (Creating Palm Pilot Software Using J2ME). But the exciting part of J2ME are the companies that have banded together to create the MIDP — the Mobile Information Device Profile — an implementation of user interface and networking optimized for small devices such as cell phones. Companies that have worked on the MIDP include Ericsson, NEC, Nokia, NTT DoCoMo, Palm Computing, Research In Motion (RIM), DoCoMo, LG TeleCom, Samsung, and more. Taking the lead in defining the MIDP is Motorola.

Motorola's has recently released an optimized Software Development Kit (SDK) allowing programmers to create "MIDlets" that can be dropped into any J2ME-supporting Motorola device. Every cell phone rolled out by Motorola by the end of next year, such as Motorola's Integrated Digital Enhanced Network (iDen) line of devices, is slated to support J2ME. Since the Motorola SDK sticks to the MIDP and J2ME standard, it should theoretically be a trivial matter to use the same code for any Java-supporting handheld gadget, pager, cell phone, Web browser, or other device.

Motorola's J2ME SDK is only available for Windows 98 or NT machines. Eventually, this SDK will be available commercially as part of Metrowerk's Code Warrior development environment. The Motorola developer's site has everything you need to get started. Register at the site, then log in using your username and password. Choose the Tools & Resources area from the top navigation bar. Download the J2ME SDK. The ZIP file should contain a file called MOTOROLA_SDK.EXE. Run it. Note that the installer may be password protected — be sure to read the installation notes in order to get the proper password. You may need to e-mail technical support at motorolajava@risc.sps.mot.com for access.

You'll also need to be sure that the Java Developer's Kit 1.2.2 is installed. To test that everything is install properly, go to your DOS command line and type:

java -version

The result should be "Java version 1.2.2" or higher. If so, you're ready to roll. If not, you may need to adjust your PATH so that JDK1.2.2 is the earliest directory in the list.

The heart of every MIDlet is the startApp() method. It kicks the MIDlet into action and should set up all the components or other objects you are interested in. Every MIDlet must also contain a pauseApp() and destroyApp() methods, which are called if and when a user chooses to either pause or quit your program.

Graphically speaking, the actual screen on your cell phone or other devices is a Display object. Every MIDlet has one and only one Display object, which you can access using getDisplay(this) . The most common object to add to the Display is a Form which can contain common input elements such as strings, text fields, radio buttons, checkboxes, and interactive gauges.

So the quick and dirty way to create a Hello World MIDlet would create a form and add it to the display using Display's setCurrent() method:

package com.mot.j2me.midlets.hello;

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

public class HelloWorld extends MIDlet {

     private Form aForm;
     private Display theDisplay;

     HelloWorld() 
     {
        theDisplay = Display.getDisplay(this);
        aForm = new Form("Hello World");
     StringItem str = new StringItem("Hello World","And Welcome!");
         aForm.append(str);
     }

     protected void startApp() throws MIDletStateChangeException 
     { 
   theDisplay.setCurrent(aForm);
     }

     protected void pauseApp() { }
     protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {
    }
}

To program such a MIDlet you could create a directory called hello beneath the MotoSDK\demo\midlets\com\mot\j2me\midlets directory. You would then create a text file called HelloWorld.java in this directory. The reason we're using such as long package/directory name is to take advantage of some nice batch files that Motorola has been kind enough to write for us.

For example, you can compile and preverify the HelloWorld MIDlet by switching to the MotoSDK\demo\midlets directory and typing:

compileAll com\mot\j2me\midlets\hello\*.java

If all goes well, you'll wind up with a fresh, happy HelloWorld.class file in the MotoSDK\demo\midlets\com\mot\j2me\midlets directory. To test the application out, Motorola has been kind enough to include an emulator. Assuming you installed everything in the \MotoSDK directory, move to the \MotoSDK\scripts directory and type:

runMotoiDEN com.mot.j2me.midlets.hello.HelloWorld

Figure 1.

You'll see your MIDlet, as in Figure 1.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel