Last week we considered the different mobile development technologies available. This week we will focus specifically on possibly the most widely used: Java ME or Java Micro Edition.
Java ME is a cutdown version of the full Java programming language, cut down so that it will fit onto mobile devices with limited memory and processing power. The first thing to make clear is that there is not one single version of Java ME. There are two editions, with different target devices (ref: Sun):
A typical phone supporting Java ME would include not only the CLDC, which is referred to as a configuration, but also a profile, the most common of which is the Mobile Information Device Profile (MIDP). The difference between the two is as follows (ref: Sun):
The advantage of having both a configuration and a profile is that it allows mobile manufacturers to add different profiles (each profile might have different graphical capabilities for example) to the base configuration depending on the capabilities of the device. For example a basic phone could support CLDC 1.0 plus MIDP 1.0, while a more advanced phone with greater graphical capabilities could support CLDC 1.0 plus MIDP 2.0.
The combination of a configuration and a profile, such as CLDC and MIDP, provides the most commonly needed features in mobile development, such as a graphical user interface and date/time manipulation. However there are a series of add-ons which cover more specific requirements of mobile applications, such as 3D graphics, Bluetooth connectivity, or location based services (being able to find your position on the earth with GPS, as we talked about last week). Each add-on is referred to as a JSR or Java Specification Request, and each is numbered. Examples of JSRs include:
It is important to realise that the phones do not actually run Java programming code directly. Java programming code (of which you will see an example below) is compiled to a binary format called Java bytecode, which is more optimised to be run directly by the phone. Java capable phones include a so-called Java Virtual Machine, a piece of software which actually runs the bytecode. You can think of it as being a bit like a software version of a CPU, so you have a Java Virtual Machine running Java bytecode, rather than an Intel CPU running Intel machine code.
OK, that's enough background, now we'll move on to how to actually develop a Java ME application. As mentioned last week, you need to download a development environment to your computer. The development environment includes a graphical representation of a phone, which you can use to test out your application. For Java ME, the environment to use is the Sun Wireless Toolkit or WTK. This is available here.
Once you have downloaded and installed the WTK, run it. Select the "New Project" button and enter a name for the project. This will create a new project. Note that the WTK is not a full Integrated Development Environment: you need to write your source files in a text editor like Notepad; but it does allow you to compile and run your Java code.
Here is the Hello World for Java ME:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet
{
TextBox txtName;
public HelloWorld()
{
txtName = new TextBox("Enter your name","",50,TextField.ANY);
}
public void startApp()
{
Display display = Display.getDisplay(this);
display.setCurrent(txtName);
}
// Midlets must have this even if blank - code to handle
// pausing the application
public void pauseApp()
{
}
// Likewise midlets must have this even if blank - code to handle
// destroying the application
public void destroyApp(boolean unconditional)
{
}
}
To explain this code:
public class HelloWorld extends MIDletYou can think of HelloWorld as being the name of the program.
Display display = Display.getDisplay(this);
So how can we react to user input? The next example demonstrates this:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Ex2 extends MIDlet implements CommandListener
{
TextBox name;
Command ok, quit;
public Ex2()
{
name = new TextBox("Name","",50,TextField.ANY);
ok = new Command("OK", Command.OK, 0);
quit = new Command("Quit", Command.EXIT, 0);
name.addCommand(quit);
name.addCommand(ok);
name.setCommandListener(this);
}
public void startApp()
{
Display display = Display.getDisplay(this);
display.setCurrent(name);
}
public void commandAction(Command c,Displayable s)
{
if(c.getCommandType() == Command.EXIT)
{
notifyDestroyed();
}
else if (c.getCommandType() == Command.OK)
{
Alert a = new Alert ("Hello!",
"Hello " + name.getString(), null, AlertType.INFO);
a.setTimeout(2000);
Display.getDisplay(this).setCurrent(a);
}
}
// Midlets must have this even if blank - code to handle
// pausing the application
public void pauseApp()
{
}
// Likewise midlets must have this even if blank - code to handle
// destroying the application
public void destroyApp(boolean unconditional)
{
}
}
To explain the new features of this code:
name.setCommandListener(this);sets the command listener for the text box. The command listener is the object that will react to the user clicking OK or Quit. Here, we specify this to indicate that it is the current object (i.e. the MIDlet) which will act as the command listener.
"Hello " + name.getString()which is the text "Hello" plus the text currently inside the text box name;
Download the WTK (from here) and install it to your computer; the C: drive is probably best.
if (text.equals("hello"))
{
// do something
}