How to Write a Java MIDlet Program for Wireless Cellular Phones and Handheld PDAs Using J2ME
|
Abstract
This article describes how to generate PRC software files for a Palm Pilot PDA that supports java. This describes how to create .jar and .jad files, often referred to as MIDlets, that could be converted to PRC files using J2ME (Java 2 Micro Edition) Wireless Tool kit and Java source files. Such PRC files created by J2ME Wireless Toolkit can be easily installed and run on a Palm PDA like any other application by using the HotSync Manager.
Procedures
- Download and install J2SE (Java 2 Platform, Standard Edition) from the Sun Developer Network Web site.
- Download and install J2ME WTK (Java 2 Platform, Micro Edition Wireless Tool Kit) from the Sun Developer Network Web site. J2SE needs to be installed prior to the J2ME WTK installation.
- Create a J2ME Java source file on any basic editor such as Notepad or Wordpad and save the file with a .java extension.
- Run the J2ME WTK (wireless toolkit) application program available in the bin subdirectory of the J2ME WTK install folder; for example, if J2ME WTK is installed in c:\WTK104, the application program is available at c:\WTK104\bin\ktoolbar.exe.ktoobar.exe. It looks like Figure 1 when it runs.
- Select the New project button and type in a project name for the MIDlet program. Type the MIDlet class name as exactly the name of the Java class that is going used by this project as shown in Figure 1 and click the Create Project button to create a new project. A new project, named SampleProject, will be created in the apps sub folder of the J2ME install directory.
Figure 1
Here is a sample j2me source file:
//SampleMidlet.java
//Sample midlet program that could be converted to PRC files
//that install into a Palm PDA.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.* ;
import java.lang.*;
public class SampleMidlet extends MIDlet implements
CommandListener
{
//member variables
private Display dCurrentDisplay;
private Form mMainMenu;
private List lChooselist;
private Ticker tMainTicker;
private TextBox txtInputBox;
private DateField dDate = new DateField("Today : ",
DateField.DATE);
//commands used, such as button, labels, and screens
static final Command cBack =
new Command("Back",Command.BACK,0); //back button
static final Command cExit =
new Command("Exit",Command.STOP,2); //Exit button
static final Command cTransfer =
new Command ("Transfer",Command.SCREEN,3);
//variables
String CurrentMenu = null;
String Text = null;
//constructors
public SampleMidlet()
{
mMainMenu = new Form("");
tMainTicker = new Ticker("Welcome To Sample MIDlet");
}
//-----------------------------------------------------------
//start application method -- called initailly while running
public void startApp() throws MIDletStateChangeException
{
dCurrentDisplay = Display.getDisplay(this);
lChooselist = new List("",Choice.IMPLICIT);
lChooselist.append("Say Hello",null);
lChooselist.append("Get Input",null);
lChooselist.append("Print Input",null);
lChooselist.addCommand(cExit);
lChooselist.setCommandListener(this);
lChooselist.setTicker(tMainTicker);
mainmenu();
}
//-----------------------------------------------------------
//mainmenu method to display the option list
void mainmenu()
{
dCurrentDisplay.setCurrent(lChooselist);
CurrentMenu = "Main";
}
//-----------------------------------------------------------
//PauseApp method to stop the program
public void pauseApp()
{
dCurrentDisplay = null;
lChooselist = null;
tMainTicker = null;
mMainMenu = null;
txtInputBox =null;
}
//-----------------------------------------------------------
public void ShowHello()
{
txtInputBox = new TextBox(" ","",50,TextField.ANY);
txtInputBox.addCommand(cBack);
txtInputBox.setCommandListener(this);
txtInputBox.setString("HELLO WORLD");
dCurrentDisplay.setCurrent(txtInputBox);
CurrentMenu = "Hello";
}
//-----------------------------------------------------------
public void GetInput()
{
txtInputBox = new TextBox("Enter Text:
","",50,TextField.ANY);
txtInputBox.addCommand(cBack);
txtInputBox.setCommandListener(this);
txtInputBox.setString(" ");
dCurrentDisplay.setCurrent(txtInputBox);
CurrentMenu = "Input";
}
//-----------------------------------------------------------
public void PrintInput()
{
txtInputBox = new TextBox("Here is what You typed",
"",50,TextField.ANY);
txtInputBox.addCommand(cBack);
txtInputBox.setCommandListener(this);
txtInputBox.setString(Text);
dCurrentDisplay.setCurrent(txtInputBox);
CurrentMenu = "Print";
}
//-----------------------------------------------------------
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
//-----------------------------------------------------------
//Handle List Events
public void commandAction(Command c, Displayable d)
{
String sLabel = c.getLabel();
if(sLabel.equals("Exit"))
{
destroyApp(true);
}
else if(sLabel.equals("Back"))
{
if(CurrentMenu.equals("Input"))
{
Text = txtInputBox.getString();
mainmenu();
}
if(CurrentMenu.equals("Hello"))
mainmenu();
if(CurrentMenu.equals("Print"))
mainmenu();
}
else
{
List nIndex = (List)dCurrentDisplay.getCurrent();
switch(nIndex.getSelectedIndex())
{
case 0:ShowHello();
break;
case 1:GetInput();
break;
case 2:PrintInput();
break;
default:break;
}
}
}
//-----------------------------------------------------------
} //end program
J2ME displays a message saying a new project is created and a folder named SampleProject will be created in the apps subdirectory in the install folder.
Figure 2
On success, the SampleProject.jar and SampleProject.jad files will be created in the bin subdirectory of the SampleProject folder.
Figure 3
Figure 4
Figure 5
Conclusion
This article focuses on creating MIDlets and converting MIDlet files into PRC files to be downloaded to the Palm Device. The above described SampleProject J2ME MIDlet package could be easily converted and installed to any modern wireless cellular or other PDAs available on the market today as long as it supports J2ME and MIDP (Mobile Information Device Profile) from Sun Microsystems; such items are available from Motorola, Nokia, Samsung, Panasonic, and so forth.
References
- Sun Microsystems Web site: http://www.sun.com.
- Sun Microsystems Developers Web site: http://developers.sun.com.
- Palm Developer's Network Web site: http://www.palm.com.
|
Partners
More for Developers
Top Authors
- Voted: 13 times.
- Voted: 11 times.
- Voted: 11 times.
- Voted: 10 times.
- Voted: 8 times.
- Paul Kimmel 214 articles
- Zafir Anjum 120 articles
- Tom Archer - MSFT 83 articles
- Mark Strawmyer 69 articles
- Bradley Jones 59 articles

