|
Hello World on Motorola RAZR Mobile Phone Using J2ME If you are like me and cant wait to write your own little applications for your new generation mobile phone, then read on to see how I went about writing and installing my own application on a Motorola RAZR phone. Motorola RAZR phone was used in the example below, but most of the steps will apply to any of the newer phones that supports J2ME applications. Please check your phone's specs or search on the net to see if your phone supports J2ME, or specifically CLDC 1.0/MIDP 2.0 or later. I am using Windows XP (with Cygwin) but the steps should work fine on Linux as well. Tools and Dependencies The tools to build and test your J2ME application is free. Copying your application and installing to your phone is the only part you will need to do some extra effort. Unless you have the data cable and application installation tools for your phones (which are usually not free), you wil need to use the OTA method of installation. OTA or Over The Air installation requires you to put your application jar on a public website and then use the phone web browser to install it over the air. Which means you will have to pay few dollors to your phone service provider for the data connection charges for the download. You can use the free hosting provided by hostj2me for OTA install of your test applications. If you have your own webserver then you can host it there. Even if you dont have access to a public webserver you can always run one on your home system and use the public IP from your internet provider.
More on adding the required MIME types to your webserver configuration
1. Download and install Sun Java Wireless Toolkit Get it from Sun's website : Sun Java Wireless Toolkit for CLDC, installs by default into C:\WTK25. 2. Use KToolbar and create a new project Start KToolbar All Programs->Sun Java Wireless Toolkit->KToolbar Select "New Project" and choose the project (Hello) and class name(HelloWorld)
In the popup project properties choose the defaults by clicking OK, unless you want to change the MIDP and CLDC versions to match your phone.
The KToolbar creats a skeleton project directory at C:/WTK25/apps/Hello
3. Write the Hello World Application go to C:/WTK25/apps/Hello/src and create HelloWorld.java using your favourite code editor. I am using vi on a Cygwin shell.
$cd C:/WTK25/apps/Hello/src
Use the following simple code to paint the hello world string in the center of the phone screen
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet {
private Display display;
private helloCanvas canvas;
public HelloWorld(){
canvas = new helloCanvas();
display = Display.getDisplay(this);
}
protected void startApp() {
display.setCurrent(canvas);
}
protected void destroyApp(boolean unconditional) { }
protected void pauseApp() { }
class helloCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString("Hello World", getWidth()/2,
getHeight()/2, g.TOP | g.HCENTER);
}
}
}
Or you can choose an even simpler text box based version.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloWorld extends MIDlet {
private Display display;
private Screen screen;
public HelloWorld() {
display = Display.getDisplay(this);
screen = new TextBox("Hello",
"Hello World", 50, TextField.ANY);
}
protected void startApp() {
display.setCurrent(screen);
}
protected void destroyApp(boolean unconditional) { }
protected void pauseApp() { }
}
4. Build and test the application From KToolbar select Build, which will compile and build your application.
From KToolbar select Run to run your application.
Which will bring up our application on a phone emulator, which is part of the Wireless Tool Kit
Click on the "Launch" buttin to run the application on the emulator.
The applications runs and you see the "Hello World" message diaplyed in graphics or in a text box, depending on the version of source code you chose.
5. Package and install the application From KToolbar select Project->Package->Create Package
This will create :
C:/WTK25/apps/Hello/bin/Hello.jad
Copy these two files to your hostj2me account, and they will provide you a URL to install your uploaded application. From your phone's browser access this URL and you will be prompted through your application install on the phone. The application then gets listed under your phones application or games listing. On Motorola RAZR from Cingular it gets listed uder games. Direct installation of application using data cable For Motorola RAZR all you need is a mini USB cable (A Male To Mini 5-Pin Male) which costs around 10$ or less and a set of tools freely avaiable at MOTODEV. Look for Motorola UID Extraction Tool and Motorola MIDway Tool (Includes the USB drivers). You will first use the UID tool and enable Java Application Loader (JAL) feature on the handset, then use MIDway Tool to install the midlet jad/jar files directly from the PC. Hosting the application on your own webserver for OTA You will need to add two extra MIME types to your webserver. If you are running Apache add these either to your apache config file or to a local .htaccess file where you put the .jad and .jar files. AddType application/java-archive .jar AddType text/vnd.sun.j2me.app-descriptor .jadMotorola RAZR web access issues On my Cingular Motorola RAZR, the direct web access from the main web link takes you to Cingular Medianet landing page, but then trying to go to any page randomly fails with "Page Cannot be Displayed" error. This is a known issue, and the easy work around is to go to Tools->Web Access->Open URL directly bypassing the MediaNet.
|
| Friday, 05-Jan-2007 11:28:32 PST | kishan at hackorama dot com |

