8.6 Developing Java Application for J2ME


8.6 Developing Java Application for J2ME

This section shows the concepts, architecture and development of a J2ME pervasive application. For design considerations and details on this technology, refer to 7.7, "Embedded mobile client applications" on page 151.

J2ME applications are not exercised in this book's sample application, but they can provide the same functionality as the previous fat client, although in this case, it is also available for smartphones besides the PDAs.

8.6.1 Developing a Midlet

In this part, we explore CDLC and MIDP API in more detail. We choose this profile because it has became more popular than others.

A Midlet is an abstract class that is subclassed to form the basis of the application. The Midlet class resides in the package javax.microedition.midlet.

Example 8-10 is quite simple; it shows the Midlet and does nothing other than putting a short text message on the screen.

Example 8-10: Midlet class
start example
 package com.itso.test; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class DemoMidlet extends MIDlet {     private TextBox textbox;     public DemoMidlet() {         textbox = new TextBox("", "UUUUUUAaaaa", 20, 0);     }     protected void startApp() throws MIDletStateChangeException {         Display.getDisplay(this).setCurrent(textbox);     }     protected void pauseApp() {     }     protected void destroyApp(boolean flag) throws MIDletStateChangeException {     } } 
end example
 

When a device receives a message to start a Midlet , the Midlet is instantiated and the application management service on the device calls the startApp() method. At this point, our application takes over control and performs any initialization that may be required.

The startApp() method can be called a number of times during the lifecycle of a Midlet. It can be placed in a pause state as a result of a call to the pauseApp when the user , or the device needs to perform some action such as writing content.

The user can choose to close the application, or for some reason the system can also require the application to close. The method destroyApp is called.

The next step is to compile the code and create the deployment files. This task can be automated by ANT or you can use WebSphere Studio Device Developer to do it for you.

Example 8-11 shows an ANT script for generating the deployable code.

Example 8-11: Steps to compile the Midlet
start example
 <?xml version="1.0" encoding="UTF-8"?> <project default="all" name="HelloMidletProject">     <target depends="updatejad palm68k/HelloMidletSuiteName" name="jxe2prc palm68k/HelloMidletSuiteName">         <jxe2prc applicationName="DemoMidlet" apptype="midp"             compress="true" creatorId="0001" midp="true"             outputFile="palm68k/HelloMidletSuiteName.prc"             platform="palm68k" scale="" translate="true">             <efileset dir="palm68k">                 <include name="HelloMidletSuiteName.jxe"/>             </efileset>         </jxe2prc>     </target>     <target depends="includemidlets palm68k/HelloMidletSuiteName" name="smartlink palm68k/HelloMidletSuiteName">         <jxelink optionsFile="palm68k/HelloMidletSuiteName.jxeLinkOptions"/>     </target>     <target name="includemidlets palm68k/HelloMidletSuiteName">         <includemidlets jadFile="HelloMidletSuiteName.jad"/>     </target>     <target depends="smartlink palm68k/HelloMidletSuiteName" name="updatejad palm68k/HelloMidletSuiteName">         <updatejad inputJadFile="HelloMidletSuiteName.jad"             jarFile="palm68k/HelloMidletSuiteName.jxe" outputJadFile="palm68k/HelloMidletSuiteName.jad"/>     </target> </project> 
end example
 

This script compiles the code for PalmOS.

The WebSphere Application Device Developer has tools to emulate this program, or you can download a simulator for the device to test this program.




Patterns. Pervasive Portals
Patterns: Pervasive Portals Patterns for E-Business Series
ISBN: 0738427772
EAN: 2147483647
Year: 2002
Pages: 83
Authors: IBM Redbooks

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net