Complementary Mobility Beginning application development with MIDP Table of Contents Introduction .............................................................................................................................................. 2 Mobile Development Considerations ....................................................................................................... 2 Preparing your first MIDP application ...................................................................................................... 3 A simple MIDP application ........................................................................................................................ 4 Working through the application.............................................................................................................. 6 Building and Testing an Application.......................................................................................................... 9 2: Beginning application development with MIDP Page: 1 Complementary Mobility Beginning application development with MIDP Introduction The purpose of this document is to explain how to create a basic application Midlet using Java and the MIDP/CLDC libraries. Java MIDP/CLDC applications are typically called Midlets and an introduction to MIDP,CLDC and installing the development environment can be found in part one of this Complementary Mobility series. This introduction assumes you are using Windows XP as your host PC operating system. Articles in this series include: 1: Introduction to Mobile Application Development 2: Beginning application development with MIDP 3: Transferring an application Midlet to a mobile device 4: Building an interactive application Midlet with MIDP 5: Building a multimedia application with MIDP Mobile Development Considerations Java MIDP/CLDC applications differ from standard Java applications primarily because they often have very specific size limitations, for example .jar files (the deployed Java Midlet) can need to be as low as 10 kilobytes in size. Different mobile devices can also have differing hardware requirements such as a grey or mono screen, a small screen, screens with different orientation options, and different keyboard layouts including touch screens. With this in mind a number of design and optimisation techniques need to be considered to minimise any unnecessary overhead that a Java MIDP application may incur. It may be useful to have particular devices in mind when designing Midlet applications but the ideal Midlet is one that works on the broadest range of devices. Due to the memory limitations of Midlets it can be useful to store more than one graphic in a file which will save space by reducing the number of times the image header is stored and reducing the space taken up by the palette (in files such as GIF that store palette information). Flickering can also be a problem as Java has a tendency by default to completely clear the screen prior to a screen refresh. A workaround for this exists using a technique called double buffering which will be discussed in a later article. File access on a mobile device is typically quite slow compared to a desktop computer. It may be prudent to load files in early on and avoid temptation to load files on the fly during program execution. 2: Beginning application development with MIDP Page: 2 Complementary Mobility Beginning application development with MIDP Preparing your first MIDP application This section assumes you are familiar with basic Java development. A simple framework will be presented and broken down through the important aspects of what makes a Java MIDP application different to a standard Java application. The first step is to select New Application in the Wireless Tooklit and enter a project name and Class name. For this introduction the Project Name and Class Name will be myApp. Clicking Create Project will create a folder structure similar to that shown in the following diagram. Following this you will need to either type in the program code below or copy and paste the code into a text file and save the file as myApp.java in the source (src) folder. 2: Beginning application development with MIDP Page: 3 Complementary Mobility Beginning application development with MIDP A simple MIDP application The program code for this application is called myApp and presents a simple text form with soft buttons for starting an application, quitting a running application (returning the application to the start) and exiting an application (removing the application from memory). The aim of the program is simply to demonstrate how to build and compile a basic MIDP framework application. This application will become the first building block for future development of MIDP applications. // A Simple Java Midlet // import necessary libraries import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; // Class for myApp application // Uses CommandListener public class myApp extends MIDlet implements CommandListener { // static variables public static Display myDisplay; public static Form myForm; public static Command myCommandStart; public static Command myCommandExit; public static Command myCommandQuit; public static int iMode; public myApp() { } // myApp public void startApp() { myDisplay = Display.getDisplay(this); myCommandStart = new Command("Start", 1, 1); myCommandExit = new Command("Exit", 1, 2); myCommandQuit = new Command("Quit", 1, 3); myForm = new Form("myApp"); myForm.append("myApp application started"); myForm.addCommand(myCommandStart); myForm.addCommand(myCommandExit); iMode = 0; myForm.setCommandListener(this); myDisplay.setCurrent(myForm); } // startApp public void sleep(int iTime) { try { Thread.sleep(iTime); } // try catch(Exception exception) { } } // sleep public void commandAction(Command command, Displayable displayable) { // check if command button pressed if(command == myCommandExit) { notifyDestroyed(); 2: Beginning application development with MIDP Page: 4 Complementary Mobility Beginning application development with MIDP } // if if(command == myCommandQuit) { myForm.removeCommand(myCommandQuit); myForm.addCommand(myCommandStart); myForm.addCommand(myCommandExit); iMode = 0; } // if // check if start button pressed if(command == myCommandStart) { // start application logic myForm.removeCommand(myCommandStart); myForm.removeCommand(myCommandExit); myForm.addCommand(myCommandQuit); iMode = 1; } // if } // commandAction // ---------// pauseApp // ---------public void pauseApp() { } // pauseApp // ---------// destroyApp // ---------public void destroyApp(boolean flag) { } // destroyApp } // myApp 2: Beginning application development with MIDP Page: 5 Complementary Mobility Beginning application development with MIDP Working through the application When working through the program you will notice that three key methods exist and they are processed as shown in the diagram below. These methods are startApp(), pauseApp() and destroyApp(). startApp() Processed the first time the application is run or when returning from a pauseApp call pauseApp() Processed when the application is initially constructed, needs pausing or goes to background destroyApp() Processed when the application is terminated You should remember that startApp() may be called many times during the running of your application, due to resuming from a pauseApp() call. With this in mind you should only place code in this section that you expect to run multiple times. Initial file loads, one-time initialisation or other similar logic should really be placed in the constructor. 2: Beginning application development with MIDP Page: 6 Complementary Mobility Beginning application development with MIDP Many of the Classes we use in this example derive from the javax.microedition.lcdui Class as shown in the following diagram. These high-level Classes make it easier for development of applications as no low-level knowledge of a particular device is required. Our application uses the Display Class which manages both the display and any input to the device. The first task asked of the Display Class is to give us a unique pointer to the Display object – to do this we call the method getDisplay(). // get pointer to the display myDisplay = Display.getDisplay(this); Once we have this we need to tell our program that we want to respond to commands. We already defined a CommandListener for use within our application so we need to decide what commands to listen out for, these are Start, Quit and Exit. Although we have defined them here the CommandListener will not listen for them in until we attach them to the CommandListener. // create commands for commandlistener myCommandStart = new Command("Start", 1, 1); myCommandExit = new Command("Exit", 1, 2); myCommandQuit = new Command("Quit", 1, 3); 2: Beginning application development with MIDP Page: 7 Complementary Mobility Beginning application development with MIDP Having told our application to watch out for commands we should probably think about putting some information on the screen. For this we have two options either the Screen Class or the Canvas Class. The Canvas Class is a low-level API which allow for very finely grained control of pixels (dots) on the device display. This is ideal for game based applications however it requires slightly more information and understanding from the developer to get the best out of it. For our simple application we can let Java take care of all the details and instead create a Form and place lists and textboxes upon it. A Form is a container that can objects such as textfields, imageitems, lists, and choicegroups. To create our Form we will use the high-level Screen Class, in particular the Form Class. // create the form myForm = new Form("myApp"); Once the Form is created we can attach our previously defined Commands and link the form to our CommandListener using the addCommand and setCommandListener methods. We will also place some text on the form using the append method. // add some text to the form myForm.append("myApp application started"); // add our commands to the form myForm.addCommand(myCommandStart); myForm.addCommand(myCommandExit); // attach the form to the command listener myForm.setCommandListener(this); The final step is to tell the device to display the form using the setCurrent method. // display the current form myDisplay.setCurrent(myForm); The other method in our program is the commandAction method. Attaching a CommandListener to our application has told it that we want to be informed about certain events such as a soft button being pressed. Alternatively, we could have used an ItemStateListener that returns information about a specific item we may have placed on our form. The CommandListener, upon receiving an event, will call commandAction method. We then have to decide what to do with these events. public void commandAction(Command command, Displayable displayable) { // check if start button pressed if(command == myCommandStart) { // start application logic } // if } // commandAction In our sample program, if we had several possible forms we could use a condition to check that (displayable == myForm) to be more specific about command processing. 2: Beginning application development with MIDP Page: 8 Complementary Mobility Beginning application development with MIDP Building and Testing an Application After placing the program code into the source folder the next stage is to build the program. This can be achieved by clicking on the Build icon on the toolbar in the toolkit, as shown in the diagram. If the program does not build successfully you may need to check it has been entered correctly and placed into the correct folder. Messages in the toolkit output window will guide you. Once the program is built successfully you will receive the Build complete message in the output windows of the toolkit interface. You can now Run your application in the emulator by clicking on the Run button in the toolkit interface. Clicking Run will bring up an emulator where you can select the Launch button to start the application Midlet. The emulator will show the application running and be waiting for you to click on the relevant soft button. In Summary Create a project Put the source code into the source folder under the project folder Compile the program Understand the purpose of startApp(), pauseApp() and destroyApp() Build the program 2: Beginning application development with MIDP Page: 9 Complementary Mobility Beginning application development with MIDP Test the program in the emulator 2: Beginning application development with MIDP Page: 10