Mobile application development J2ME U N I T I I
Overview J2Me Layered Architecture Small Computing Device requirements Run Time Environment Java Application Descriptor File Java Archive File MIDlet Programming Life cycle methods Event Handling User Interfaces & Device Data Java Language for J2ME J2Me software development Kits Hello World J2ME Style J2ME Wireless Tool kit
J2ME Architecture MIDP Applications OEM-Specific Applications Native Applications MIDP OEM-Specific Classes CLDC Native System Software/Host Operating System Mobile Information Device (MID)
J2ME Architecture J2ME architecture enables an application to be scaled on constraints of small computing device J2ME architecture consists of layers located above the native operating system collectively referred to as the connected Limited Device Configuration(CLDC) and forms the runtime environment. It consists of three software layers JVM Directly interacts with native operating system Profile consists of minimum set of APIs MIDP contains java API s for user network connections, persistent storage and the user interface. OEM Original Equipment Manufacturer has two components OEM classes used by MIDP to access device specific features such as SMS etc OEM applications programs provided by OEM such as address book
J2ME Architecture CLDC java.lang java.io java.util javax.microedition.io MIDP javax.microedition.lcdui javax.microedition.midlet javax.microedition.rms
Small Computing Device Requirements Hardware Display Memory Input Device Connectivity Minimum Requirement 96 X 54 Pixels 65,536 colors 32 KB workspace 128KB MIDP Implementation 8KB for MIDlets run The device has the equivalent of a keypad that allows the user to type the numbers 0 through 9,together with the equivalent of arrow keys and a select button HTTP 1.1; TCP/IP
Run Time Environment 7 One or more MIDlets are packaged together into a MIDlet suite, composed of: JAR (Java archive) file JAD (Java Application Descriptor) file All the user-defined classes and resources required by the suite's MIDlets must be in the JAR file. J2ME
JAR & JAD The JAR file must also include a manifest that describe the MIDlets in the suite. 8 The application descriptor (JAD) contains similar information, and is used by devices to obtain information about a MIDlet suite without having to download and install the MIDlet suite first. J2ME
JAD.. Used by AMS to manage MIDlets Has to be supplied to run a CLDC-compliant app File extension is. jad Some attributes of the manifest file are duplicated in the JAD MUST match information in the JAR Manifest otherwise MIDlet won t load The attributes are Name/Value pair, separated by a colon and optional whitespace
Sample JAD MIDlet-1: MyMIDlet, MyMIDlet.png, MyMIDlet MIDlet-2: FirstConnection,, FirstConnection MIDlet-Jar-Size: 100 MIDlet-Jar-URL: MyMIDlet.jar MIDlet-Name: MyMIDlet MIDlet-Vendor: Switchback Software MIDlet-Version: 1.0 MicroEdition-Configuration: CLDC-1.1 MicroEdition-Profile: MIDP-2.0
MIDlet Programming MIDP applications are known as MIDlets MIDlet is the basic application Similar to the J2SE applet GUI based MIDlet Suite Grouping of related MIDlets as a single unit All the MIDlets of a MIDlet are executed as a single instance security for applications that share resources or data
MIDP Application Lifecycle MIDlets move from state to state in the lifecycle, as indicated. Pause Start acquire resources and start executing Pause release resources and become quiescent (wait) Destroy release all resources, destroy threads, and end all activity pauseapp startapp Active destroyapp destroyapp Destroyed
Lifecycle Methods startapp() Can be called multiple times, need to prevent subsequent calls to startapp() from starting the UI at the beginning a wizard pauseapp() usually used to release any resources that we re holding on too. destroyapp() any cleanup, prevents the user from exiting the app if a submission of data is in progress. Screen interactions are handled by the commandaction() which we ll talk about shortly...
startapp() Starts the applications, called from the AMS Maybe called multiple times, so needs to account for both initial and normal running semantics Should try and return quickly because the applications Displayable is not shown until after this method exits. Throws MIDletStateChangeException if app can t start now, but might be able to start at a later time. Prevents subsequent calls to startapp() from starting the UI at the beginning of the wizard
startapp() protected void startapp() throws javax.microedition.midlet. } MIDletStateChangeException { if (false == this.m_binitialized) { } m_binitialized = true; startui();
MIDP Mobile Information Device Profile Targeted at medium- to low-end cell phones and two-way pagers Spec addresses issues such as Application life cycle User interface Persistence storage Networking
MIDlets MIDP application on a device that is managed by the AMS MIDlet suite is a JAR file that contains 1 or more MIDlets MIDlet-<n> contains info about that MIDlet MIDlet-1: MyMIDlet, MyMIDlet.png, MyMIDlet MIDlet-2: FirstConnection,, FirstConnection <n> MIDlets get packaged in the same JAR file AMS gives choice for loading each MIDlet
Sample Code import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class HiMIDlet extends MIDlet { private TextBox textbox; public HiMIDlet() { textbox = new TextBox ("", "Hello World!", 20, 0); } public void startapp() { Display.getDisplay(this).setCurrent(textbox); } public void pauseapp() { } public void destroyapp(boolean unconditional) {} }
Event Handling of J2ME A MIDlet is a event based application When a user interacts with a MIDlet, events are generated and the application is modified to handle and respond to these events. The application is notified of such events through callbacks. All routines executed in the MIDlet are invoked in response to an event reported to the MIDlet by the application manager The initial event that occurs is when the MIDlet is started and the application manager invokes the startapp() method Each screen is associated with CommandListener interface A CommandListener monitors user events with screen and causes the appropriate code to be executed based on the current event.. Screen events comes under High level events Low-level events represent single key presses and releases
J2ME User Interfaces The Design of user interface for a MIDlet depends on the restrictions of small computing device There are two types of user interfaces supported by J2ME High Level UI (Screen ) Low Level UI (Canvas ) A Form is the most commonly invoked user interface A user enters information into for by using Choice element, TextBox, TextField, DateField elements. Other user interface elements are StringItem contains text List = contains List of Items Alert to alert the user Ticker- scrollable text etc..
J2ME Device Data Small computing devices don t have the resources necessary to run an onboard database management system (DBMS) A MIDlet uses RecordStore MIDP class and two MIDP interfaces called RecordComparator and RecordFilter to write and read persistent data. RecordStore class contains methods used to write and read persistent data.
Java Language for J2ME CDC implements the full J2SE available but CLDC implements stripped down J2SE due to resource constraints 1. Floating point math is most notable feature of J2ME which requires extra hardware 2. J2ME does not support finalize() method 3. Reduced number of error handling exceptions are supported in J2ME.because exception handling drains system resources 4. JVM for small computing devices requires a custom class loader supplied by the device manufacturer 5. J2ME does not support ThreadGroup class 6. References and Reflection classes are not supported by J2ME 7. J2ME does not support Security Manager instead it uses class file verification to protect applications with a two step process 1. Pre verification -- outside the device and 2. verification --after loading the MIDlet into the device
J2ME Software Development Kits A MIDlet is built using free software packages that are downloadable form java.sun.com Three Packages are needed 1. Java Development Kit (1.3 or greater ) java.sun.com/j2se/downloads.html - It contains java compiler and jar.exe file which is used to crate Java achieve file (JAR ) 2. Connected Limited Device Configuration(CLDC) java.sun.com/products/cldc/ -unzip CLDC on to d:\j2me directory - CLDC package creates j2me_cldc sub directory under j2me - j2me_cldc has bin subdirectory that contains KVM and preverifier executable files - add j2me\j2me_cldc\bin\win32 subdirectory to PATH environment variable 3. Mobile Information Device Profile (MIDP) java.sun.com/products/midp/ - unzip MIDP file creates a midp directory1.0.3fcs directory which also contains bin sub directory - add j2me\midp1.0.3fcs\bin subdirectory to PATH environment variable
J2ME Software Development Kits Set the CLASSPATH to d:\j2me\mipdp1.0.3fcs\classes Set the MIDP_HOME environment variable to d:\j2me\midp1.0.3fcs.
Hello World J2ME Style The directories used to develop HelloWorld MIDlet J2me J2me\src J2me\src\greeting\j2me\tmp_classes (greetings is the user defined directory) J2me\midlets Steps to Run HelloWorld MIDlet 1. Develop the source code HelloWorld.java 2. Compile HelloWorld.java 3. Pre verify the HelloWorld Class file 4. Running HelloWorld
HelloWorld MIDlet 1. HelloWorld MIDlet is created by defining HelloWorld class import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class HelloWorld extends MIDlet { private TextBox textbox; public HiMIDlet() { textbox = new TextBox ("", "Hello World!", 20, 0); } public void startapp() { Display.getDisplay(this).setCurrent(textbox); } public void pauseapp() { } public void destroyapp(boolean unconditional) {} }
HelloWorld MIDlet 2 Compilling Hello WorldelloWorld -- j2me\src\greeting is the current directory Enter the following commamnd at the command line javac d d:\j2me\tmp_classes target 1.1 bootclasspath d:\j2me\midp10.3fcs\clases HelloWorld.java The bootclasspath option must be used when compiling a MIDlet which points to the startup files( java bootstrap files ) The compiler produces HelloWorld.class in the j2me\temp_classes\greetings directory 3. Prevrify HelloWorld.class by executing the following command Preverify d d:\j2me\classes classpath d:\j2me\midp1.0.3fcs\classes d:\j2me\tmp_classes -d option places the class file within the tmp_classes directory -classpath option points to the location of the library classes that come with the MIDP
HelloWorld MIDlet 4. Running HelloWorld A MIDlet should be tested in an emulator before being downloaded to a small computing device. An emulator is software that simmulates how a MIDlet will run in a small computing device There are Two ways to run a MIDlet 1. Invoking the MIDletclass or 2. Running the MIDlet from JAR file Enter the following command for running MIDlet class without JAR file midp classpath d:\j2me\classes greeting.helloworld
Installation of Java Wireless Toolkit (J2ME) 1) If the Java Development Kit (JDK) is not there or only having the Java Runtime Environment (JRE) installed, install the latest JDK from http://java.sun.com/javase/downloads/index.jsp. Current stable release of Java is JDK 6 Update 7 2) Next, download the Java Wireless Toolkit (formerly called J2ME Wireless Toolkit) from: http://java.sun.com/products/sjwtoolkit/download.html. 3) Run the installer (for example, for Windows it is: sun_java_wireless_toolkit- 2_5_2-windows.exe). The installer checks whether a compatible Java environment has been preinstalled. If not, it is necessary to uninstall old versions of Java and perform Step 1 again.
Steps to run MIDlet in toolkit: 1. Start -> All Programs -> Sun Java Tool Kit -> Wireless Tool Kit 2. Click New Project Enter Project Name -> Enter Class Name -> Click on Create Project. The toolkit will create a project directory for you using the project name you provide. 3. Choose appropriate API Selection and Configurations. 4. Place Java Source file in WTK2.1 / WTK2.2\apps\projectname\src 5. Build the Project. 6. Run the Project.
HelloWorld Using J2ME Wireless Tool Kit import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class HelloWorld extends MIDlet { private Form form; private Display display; public HelloWorld() { super(); } public void startapp() { form = new Form("Hello World"); String msg = "Hello World!!!!!!!"; form.append(msg); display = Display.getDisplay(this); display.setcurrent(form); } public void pauseapp(){} public void destroyapp(boolean unconditional) { notifydestroyed(); } }
Sun s J2ME Wireless Toolkit To run the toolkit itself, select the KToolbar shortcut. You should see the following screen.
Sun s J2ME Wireless Toolkit The J2ME Wireless Toolkit works with projects, where the end result of each project is one MIDlet suite. The toolkit works with one project at a time. You can change properties of the current project, build the project, and run the project in a device emulator. Several example projects come installed with the toolkit; we'll look at these later. Click on New Project in the button bar. The toolkit prompts you for a project name and the name of a MIDlet class in the project. Fill in HelloSuite and HelloMIDlet as shown below.
Nokia J2ME SDK
Important Questions 1. Explain J2ME architecture with a neat diagram. 2. Develop a hello world program using J2ME style. 3. Explain Event processing and Data Management in J2ME 4. Explain how multiple MIDlets are distributed in a single MIDlet suite 5. Explain the java language features of J2ME 6. Explain the features of J2ME wireless toolkit