Advertisment

Writing Java Apps for Cellphones

author-image
PCQ Bureau
New Update

Java-enabled cellphones are now a common sight and so are the numerous Java-based applications for these phones. Some come bundled with the cellphones, while the others can be downloaded from the Internet. You can also write one yourself if you are familiar with Java. This article will take you through the basics of doing so and show you how to write a full-fledged Java application called PasswordStoreMIDlet that can store website logins and passwords on your

cellphone. 

Advertisment

Setting the stage



You don't need a Java-enabled cellphone to learn to write and test a Java application. All you need are the latest J2SE (Java 2 Standard Edition) and J2ME (Java 2 Micro Edition) kits from java.sun.com. J2ME is the software development kit for developing for cellphones and embedded devices. It comes with a graphical cellphone emulator, using which you can test your applications before real-world deployment. A Java application written for cellphones is called a MIDlet (just as a Java application written for a Web browser is called an applet). 

Direct

Hit!
Applies to:

Mobile application developers
USP:

How to write an application using J2ME to store data on cellphones persistently
Links: http://java.sun.com,

http://java.sun.com/j2me/index.jsp
 

The basics



As with applets, a MIDlet first needs to import some classes specific to a MIDlet, namely javax.microedition.lcdui.* and javax.microedition.midlet.*. Every MIDlet must extend (inherit) from the MIDlet class. To process events, such as the click on the cellphone keys, the MIDlet should implement the CommandListener interface (similar to the ActionListener in case of Java applications and applets). Similar to Java applications, MIDlets support forms to place the components (textfield and labels). But, the form must in-turn be placed onto the cellphone's display to be displayed. This is a two-step process: one, get a reference to the display and, two, place the form on the display. These are achieved by the following two commands:

Advertisment

Display display = Display.getDisplay (this);



display.setCurrent(

);

No buttons, only commands 



Unlike Java applications and applets, a MIDlet does not have a clickable button on the screen. What it can have is a command associated with the selection buttons (on left and right) on the cellphone. To create a command for the left button:

Command cmd = new Command(“

Advertisment

and to create a right button:

Command cmd = new Command(“

To add a command to the form:

Advertisment

form.addCommand(cmd);

To specify an event listener for the command:

form.setCommandListner(this);

Advertisment

The cellphone emulator that comes with the J2ME kit allows you to run and test Java MIDlets and even play Java games

This specifies that the class containing the form will contain the method (function)- namely commandAction( )-to execute when the selection button is clicked. This is similar to the actionPerformed( ) method for Java applications and applets. Within this function, the methodology of handling the event is the same as with any Java application. 

The scope of a MIDlet 



As applets, a MIDlet is also created, paused (stopped) and destroyed. There are methods or functions executed when each of these events occur. These are as follows.

Advertisment

startApp( ): called when the MIDlet is ready to be executed



pauseApp( ): called when the MIDlet is paused


destroyApp( ): called when the MIDlet is destroyed

This is very similar to the applet's start, stop and destroy methods.

Persistent storage for MIDlets



What if a MIDlet wants to store some data on a file to retrieve it later? In case of the PasswordStore MIDlet (You will see later), you will need to store data on persistent storage. For this, MIDlet provides a facility called RMS (Record Management System). RMS is simple to use. You need to come up with a record store name, open the record store, read/write to it using getRecord( ) and addRecord( ) methods, respectively, and, finally, close the record store. 

Advertisment

A record store is opened or created as:

RecordStore recStore = RecordStore.openRecordStore(, true );

The second parameter (true) specifies to create a record store if it does not exist. To write to the record store:

recStore.addRecord(rec, 0, rec.length);

Here rec is an array of bytes. So, if you want to write a string to the record store (as in the case of the PasswordStore MIDlet), you will have to convert the string (say str) into a byte array as:

byte<> rec = str.getBytes();

The contents/entries in a record store can be iterated using a for loop. Each entry is returned as an array of bytes using the getRecord( ) method, whose syntax is as follows.

recStore.getRecord();

Here is the entry/record number. The above statement returns the entry as an array of bytes. Using a method called recordSize( ), you can find beforehand, the number of bytes which will be returned. 

A record store is closed for reading/writing as:

recStore.closeRecordStore( );

The PasswordStoreMIDlet



You can download the source code of PasswordStoreMIDlet from the PCQuest online forum at

http://forums.pcquest.com/forum/viewtopic.php?t=431.

Open the code in a text editor while we take you through its explanation. The code starts with importing the classes required by the MIDlet. The constructor looks as follows.

public PasswordStoreMIDlet(){



display = Display.getDisplay (this);


buildForms(); 


openRecordStore();


}


The PasswordStoreMIDlet running in the J2ME emulator 

As explained before, the first line stores a reference to the cellphone's display in the 'display' object variable. The buildForm( ) method constructs a form each for the first display, input for the websites' credentials and searching through the stored credentials. We call these forms mainForm, addForm and searchForm, respectively. Note how we create textfields, commands and labels (StringItems) in the buildForm( ) method. The textfields and StringItems are added to the form using the append( ) method. For example:

StringItem msg = new StringItem(“”,”Store and search your website logins”);



mainForm.append(msg);

The commands are added to the form using the addCommand( ) method. For example:

add = new Command(“Add”,Command.OK,1);



mainForm.addCommand(add);

The statements:

mainForm.setCommandListener(this);



addForm.setCommandListener(this);


searchForm.setCommandListener(this);

specify that a method named commandAction( ) in the PasswordStoreMIDlet class will handle the command events — click on events. 

Within the methods openRecordStore( ) and closeRecordStore( ), the record store is opened for reading/writing and closed, respectively. Note that we call closeRecordStore( ) in the destroyApp( ) method, which means we close the record store when the user closes the MIDlet. 

The method writeRecord( ) writes the website login credentials to the record store as explained before. The way the website credentials are stored are: the first entry in the record store is the website name, the second entry is the login and the third entry is the password. 

The search( ) method is executed when one fills in the website to search on the searchForm and clicks on the right selection button on the search form. The search( ) method iterates through every third entry in the record store to find a match for the website searched for. If found, it displays the next two entries in the record store, which are the login and password, and breaks through the loop. 

Running the code



With J2ME installed, launch the KToolbar. Click on New Project. For 'Project Name' and 'MIDlet Class Name', enter PasswordStoreMIDlet. Click on OK on the window titled 'Settings for project'. Copy and save the code downloaded from the PCQuest forum in a file named PasswordStoreMIDlet.java. Place this file in the subdirectory

apps\PasswordStoreMIDlet\src of the J2ME installation directory. Click on Build on the KToolbar and then on Run. This will open a nice looking cellphone emulator. Click on the left and right selection buttons to navigate through the application. While adding an entry, type in the website name, your login and password. To search, specify the website name and the emulator will display the corresponding login and password from the record store. For more, refer to the home page of J2ME at http://java.sun. com/j2me/index.jsp.

Shekhar Govindarajan



IT4Enterprise

Advertisment