Vous êtes sur la page 1sur 22

F O R U M

N O K I A

Whats in MIDP 2.0: A Guide for Java Developers


Version 1.0; September 15, 2003

Java

Forum.Nokia.com

Copyright 2003 Nokia Corporation. All rights reserved. Nokia, Nokia Connecting People, and Nokia 6600 are trademarks or registered trademarks of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other product and company names mentioned herein may be trademarks or trade names of their respective owners. Disclaimer The information in this document is provided as is, with no warranties whatsoever, including any warranty of merchantability, fitness for any particular purpose, or any warranty otherwise arising out of any proposal, specification, or sample. Furthermore, information provided in this document is preliminary, and may be changed substantially prior to final release. This document is provided for informational purposes only. Nokia Corporation disclaims all liability, including liability for infringement of any proprietary rights, relating to implementation of information presented in this document. Nokia Corporation does not warrant or represent that such use will not infringe such rights. Nokia Corporation retains the right to make changes to this specification at any time, without notice. License A license is hereby granted to download and print a copy of this specification for personal use only. No other license to any other intellectual property rights is granted herein.

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

Contents
1 Introduction..........................................................................................................................................................6 1.1 2 Purpose and Scope................................................................................................................................6

A Few Definitions: J2ME Architecture.......................................................................................................7 2.1 Basic Terms..............................................................................................................................................7 2.1.1 2.1.2 2.2 2.3 Configurations ......................................................................................................................7 Profiles ....................................................................................................................................8

The Environment ...................................................................................................................................8 What MIDP Can't Do...............................................................................................................................8 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 Floating point .......................................................................................................................9 Class loader............................................................................................................................9 Finalization............................................................................................................................9 Java Native Interface (JNI).................................................................................................9 Reflection ...............................................................................................................................9 Error handling.......................................................................................................................9 High-level APIs......................................................................................................................9 File handling..........................................................................................................................9 No thread groups or daemon threads ....................................................................... 10

2.4

What MIDP Can Do .............................................................................................................................. 10 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 Core Java libraries............................................................................................................. 10 Displays................................................................................................................................ 10 High-level user interfaces............................................................................................... 10 Canvas .................................................................................................................................. 11 The new Game API ........................................................................................................... 11 RGB images......................................................................................................................... 12 Commands.......................................................................................................................... 12 MIDP 2.0 Media API........................................................................................................... 13 Expanded connectivity .................................................................................................... 13 Push architecture.............................................................................................................. 13 Timers and Alerts.............................................................................................................. 13 Record Management System (RMS)............................................................................. 14 End-to-end security HTTPS ....................................................................................... 14 Trusted MIDlet suites ....................................................................................................... 14

Putting It Together ......................................................................................................................................... 15 3.1 What Is a MIDlet? ................................................................................................................................ 15 3.1.1 3.1.2 3.2 MIDlet state......................................................................................................................... 15 Required methods............................................................................................................ 15

MIDlet Structure .................................................................................................................................. 15


3

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

3.3

Deploying Your MIDlet....................................................................................................................... 17 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 Compiling ............................................................................................................................ 17 Preverifying ........................................................................................................................ 17 Emulating............................................................................................................................ 18 Packaging............................................................................................................................ 18 Over-the-Air provisioning............................................................................................... 18

4 5

Conclusion........................................................................................................................................................... 19 References .......................................................................................................................................................... 20 5.1 5.2 5.3 Sun Documents.................................................................................................................................... 20 Tools ....................................................................................................................................................... 20 Nokia Documents................................................................................................................................ 20

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

Change History
September 15, 2003 V1.0 Initial document release

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

1 Introduction
1.1 Purpose and Scope This document is aimed at developers who have Java programming experience but are new to Java 2 Platform, Micro Edition (J2ME) and version 2.0 of its Mobile Information Device Profile (MIDP). It gives a broad outline of the differences between J2ME and regular Java technology, focusing particularly on the issues related to creating games under J2ME. Developers familiar with MIDP 1.0, the previous version of MIDP, will also find this document a helpful introduction to the new version of MIDP, but may want to skip Section 2.1, Basic Terms. As of this writing, few devices that support MIDP 2.0 are available, and most developers will prefer to develop for MIDP 1.0; a companion document, Whats in MIDP 1.0: A Guide for Developers, is also available on the Forum Nokia Web site (http://www.forum.nokia.com/). Nokia Developers Suite for J2ME, Version 2.0, supports both MIDP 1.0 and MIDP 2.0 development, and is available for download from the Tools page of the Forum Nokia Web site (www.forum.nokia.com/tools).

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

2 A Few Definitions: J2ME Architecture


The Java programming language defines a set of APIs, a virtual machine, and a technology. Most people closely associate Java technology with its standard user interface APIs, Swing, and its older brother, the Abstract Window Toolkit (AWT). But its certainly possible to write Java applications without them, and with J2ME, its also necessary. J2ME is the flavor of Java technology designed for devices with constrained resources, such as mobile phones. By comparison to Java 2 Platform, Standard Edition (J2SE), the version of Java technology for desktops, and Java 2 Platform, Enterprise Edition (J2EE), the version for servers, J2ME is quite limited, but is still a more complex technology than one might expect. Because J2ME is designed for constrained devices, the nuances of these machines must be taken into account in both the virtual machine and the APIs. 2.1 Basic Terms The fact that J2ME is aimed at a range of devices that are limited in processor power, user interface, and memory makes it hard to make a one-size-fits-all environment. Thus, Sun Microsystems defines configurations within J2ME; a configuration defines the capabilities of one set of target devices. And in addition to each configuration, Sun defines profiles, which determine the APIs and features available. J2ME is a subset of Java technology; the J2ME run-time environment is a combination of a configuration and a profile.

Figure 1: Java editions

2.1.1

Configurations

Due to the range of constrained devices J2ME is aimed at, a single definition of the platform would be insufficient. Configurations have been created to define the virtual machine and minimal set of classes that will be present across a broad range of devices.
Whats in MIDP 2.0: A Guide for Java Developers 7

Forum.Nokia.com

This document focuses on the Connected Limited Device Configuration (CLDC) 1.0; a device falling within this configuration must meet the following specifications: Hardware capability of at least 128 kB to 512 kB of RAM, 16- or 32-bit CPU Implementation of a small virtual machine known as the KVM Low bandwidth, battery operated, low power consumption, intermittent network connectivity Note: The MIDP 2.0 specification is designed to operate on top of the CLDC, both CLDC 1.0 and CLDC 1.1. 2.1.2 Profiles

We have seen that the configuration defines the underlying environment according to the physical capabilities of the device. The profile further defines the core application functionality, such as user interface, network connectivity, local data storage, and application management. For mobile devices and entry-level PDAs, this profile is known as MIDP. Therefore, the combination of CLDC and MIDP provides all of the resources required by the developer to create highly graphical games and applications, optimized for small screen sizes and mobile device input methods. 2.2 The Environment Developers wishing to code to MIDP must first acquire the tools. The best bet is to go to the J2ME Downloads page on Suns Java Web site: http://java.sun.com/j2me/download.html Developers should then download and install the latest version of the J2ME Wireless Toolkit. This requires a reasonably current edition of the J2SE run-time environment, installed and set up. In addition, if developers are targeting Nokia devices, they will want to download Nokia Developers Suite for J2ME, Version 2.0 or later. (Earlier versions do not support MIDP 2.0.) It provides a collection of additional features that can be used to make it easier to develop for the J2ME platform. Developers may also want to visit the Forum Nokia Web site and download and install SDKs for Series 40, Series 60, and/or specific Nokia devices that provide emulators and other tools for those devices. http://www.forum.nokia.com/tools Finally, developers may want to install an integrated development environment (IDE) such as Borland JBuilder (and MobileSet), available from Borlands Web site, or Sun ONE Studio, available from Suns site. http://www.borland.com/jbuilder/ http://www.sun.com/software/sundev/jde Note: These URLs are correct as of this writing. 2.3 What MIDP Can't Do The limitations of MIDP are understandable given the nature of the devices being addressed, but much of J2ME and MIDP is still defined by things developers would like to do but cant. MIDP 2.0 makes life a little easier than 1.0, but developers still need to be aware of limitations.

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

2.3.1

Floating point

The CLDC 1.0 configuration does not support floating-point numbers. Applications cannot use floatingpoint types such as float or double. Floating-point-type calculations can still be made, but the results have to be stored in a string or as an integer array. 2.3.2 Class loader

In J2SE and other larger-scale implementations of Java technology, the class loader is responsible for locating, linking, and loading Java classes needed to support an application. The class loader is also responsible for security and verification of byte codes. This requires significant processing power, and for this and security reasons, no class loader has been implemented in MIDP. Therefore, MIDP applications have to be preverified as a form of manual class loading. 2.3.3 Finalization

It is unwise to rely on finalization (the finalize() method) even under J2SE, but under J2ME, it simply doesnt exist. If an object needs to be cleaned up before its deleted, the developer must manage that process. 2.3.4 Java Native Interface (JNI)

The Java Native Interface (JNI) doesnt exist in J2ME. This is not a big problem, because it wouldnt be much help anyway small devices dont share the same CPU type, calling convention, or hardware profile, and there often is no way to download executable code into these devices. 2.3.5 Reflection

Similarly, while reflection is a very useful and powerful tool in J2SE, there are a variety of problems associated with locating, loading, and running additional classes at run time. Its understandable that reflection was omitted from the J2ME system. There is also no support for object serialization or Remote Method Invocation (RMI). 2.3.6 Error handling

Exception handling is expensive, and small devices often dont have mechanisms for reporting errors other than to crash or reset the device. Consequently, the requirements for error handling under J2ME have been relaxed. The CLDC only defines three error classes: java.lang.Error, java.lang.OutOfMemoryError, and java.lang.VirtualMachineError. 2.3.7 High-level APIs

Large user interface APIs such as Swing and AWT are too cumbersome to be handled on a small device. MIDP implements its own set of user interface APIs that take into account the capabilities of mobile devices. These classes are outlined in Section 2.4.3, High-level user interfaces. 2.3.8 File handling

There is no interface for reading or writing to persistent files on the devices file system. Instead, the developer can use the Record Management System (RMS) for persistent data storage.

Whats in MIDP 2.0: A Guide for Java Developers

Forum.Nokia.com

2.3.9

No thread groups or daemon threads

Although multithreading is possible with MIDP, it does not support thread groups or daemon threads. However, thread groups can be created at the application level by using a collection to store the thread objects. 2.4 What MIDP Can Do Even for experienced Java programmers, the J2ME platform should be viewed as a completely new programming environment. Applications must take into consideration the limited processor power and memory space limits of these devices. Care should be taken to ensure that applications close gracefully when they are no longer required. This section outlines the capabilities of the platform and what it provides. 2.4.1 Core Java libraries

The first thing developers will notice about the MIDP class libraries is that they are much smaller than their J2SE counterparts. This is because MIDP devices typically have far less available memory. J2MEs classes can be divided into two groups: those inherited as a subset of the J2SE APIs, and those new and specific to J2ME. The list is basic. Besides Object, developers will find that Class, Runnable, String, System, Thread, and Throwable are all still available from java.lang. The data-type wrapper classes are all there (for the data types that dont use floating point, that is). Basic collection classes from java.util such as Stack, Vector, and Hashtable still exist, as does Enumeration (but sadly, not Iterator). Most of the fundamental I/O Stream classes (java.io) exist, as do Date and Calendar classes. It is still possible to do random numbers with java.util.Random, and basic integer math functions such as abs, min, and max are available from java.lang.Math. As mentioned in Section 2.3.6, Error handling, there is also a limited list of standard exception classes. 2.4.2 Displays

A MIDP application has exactly one Display object, which controls access to the display and input devices on the system. Only one Display object can be current at a time. A Displayable is a container for all visible user interface objects. Displayable has two subclasses: Canvas, which allows pixel-by-pixel control of the display and is used for low-level interface features; and Screen, which is used to display high-level interface features such as selection lists and date fields. 2.4.3 High-level user interfaces

A Form is a Screen that contains a set of interface elements and handles the layout of those items, possibly including scrolling. MIDP provides a fairly general suite of standard form-based user interface components, including: ChoiceGroup, the equivalent of radio buttons and check boxes. The exact appearance and usage of a choice group depends on the particular device. TextField, which is a text component capable of various forms of input constraints. It can restrict the characters entered to particular character sets or input formats (such as a telephone number or e-mail address). DateField, which offers a couple of different specialized date and time input controls. On some machines, the DateField will present interactive calendar and time fields.

Whats in MIDP 2.0: A Guide for Java Developers

10

Forum.Nokia.com

Gauge, which is a way of showing progress or permitting the user to enter a value from within a linear range. Image, which can be used to contain arbitrary 2-D images using the PNG image format. Image objects can also be constructed as subimages of loaded images or of drawn graphics.

MIDP 2.0 adds three new components usable with Form: Spacer, which adds space when the Form is displayed and provides a little more control over how it appears. StringItem, which simply displays text that the user cannot edit (though the application can change dynamically). CustomItem, which allows developers to create custom Items for display in a Form.

MIDP 2.0 allows better control over a Form than MIDP 1.0 did; it provides the concept of layout policies for Forms, and Spacer can be used to modify appearance. For developers creating basic consumer applications, these may be all they need. These items can be placed in a Form and displayed. 2.4.4 Canvas

To display something like a game with moving or dynamically generated graphics, a developer will want to take control at a lower level, with Canvas. A Canvas is interchangeable with a Screen, since both are Displayable, making it easy to swap between a menu and the main part of the game or other application. Canvas must be subclassed to be used. It supports an abstract paint() method that is called whenever the Canvas needs to be drawn or updated. A variety of methods can be used to paint shapes, text, and so on to the Canvas; MIDP 2.0 also requires support for PNG images (with, unlike MIDP 1.0, required support for transparency). Games developers may find the more sophisticated GameCanvas, a subclass of Canvas, more useful. It has more functionality for use in games development and will be outlined next (see Section 2.4.5, The new Game API). Note: For MIDP 1.0 devices, using the Nokia UI API and its FullCanvas class provides useful facilities not available in standard MIDP 1.0. To ensure backward compatibility with legacy applications, Nokia continues to provide the Nokia UI API in MIDP 2.0 devices. Essentially, the Nokia UI API was a stopgap to redress some of the shortcomings of MIDP 1.0, and is no longer necessary with MIDP 2.0. 2.4.5 The new Game API

MIDP 2.0 (unlike MIDP 1.0) contains a new Game API, javax.microedition.lcdui.game. Essentially, it provides facilities commonly required for games animation, such as sprites and tiled backgrounds, so that developers need not code these anew for every game. The Layer is the basic class of the Game API; it is essentially a drawing surface with which to add Sprites. A Sprite is a Layer that animates image frames. One useful method of Sprite is collidesWith(), which can be used to detect object collision in a game. TiledLayer is a Layer that can be drawn from tile images supplied as a resource in the MIDlet suite. This is most useful when creating backgrounds for games, or, indeed, splash screens.

Whats in MIDP 2.0: A Guide for Java Developers

11

Forum.Nokia.com

All of these layers are managed by the LayerManager, which keeps a register of all layers and renders them to the screen. LayerManager treats Layers as, quite literally, layered, so that a nearer layer will be painted over and obscure farther layers a highly useful feature. Additionally, the GameCanvas provides a buffer for fast, flicker-free animation. The flushGraphics method is used for copying the graphical content from the buffer to the screen. GameCanvas provides a method, getKeyStates(), that is supposed to allow developers to determine the current state of all of the devices keys (either recently pressed or not pressed since the method was last called). In theory, this redresses one of the great oversights, from a games development perspective, of MIDP 1.0: The fact that most MIDP 1.0 devices dont provide any way for developers to detect simultaneous key presses, and thus allow, for example, a player to fire and move at the same time. However, despite making the method a required part of MIDP 2.0, the specification also states that some devices may be incapable of detecting simultaneous key presses, meaning such devices can still be considered MIDP 2.0-compliant. Developers will find it necessary to determine whether simultaneous key presses are detectable on a device-by-device basis. Note: The first Nokia device supporting MIDP 2.0, the Nokia 6600 phone, will detect simultaneous key presses. 2.4.6 RGB images

Also new in MIDP 2.0 is the ability to store and manipulate images as integer arrays. Developers can create their own transformations of images over and above the standard transformation supplied in GameCanvas, using the drawRGB() method: public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height, boolean processAlpha) This allows image transformation on a pixel-by-pixel level. With collision detection a part of the Game API, games developers will find this particularly useful because they will be able to change images on the fly, e.g., to show some visual response to a collision. 2.4.7 Commands

All interactions with the user take place through events or commands. Commands act as a combination of buttons and named actions. The developer specifies the set of commands available to the user, and the device maps them to user actions. Command types, such as Help, Exit, and Back, will be placed in a standard way. Commands will be mapped to hot keys on the device, or to on-screen buttons, or to a list of possible commands, depending on the particular device. Most Nokia devices use left and right soft keys. The right soft key is filled first with a single Command in priority order: Stop, Cancel, Back, or Exit. The exact behavior is device-dependent; additional information can be found in the document Java MIDP Application Developers Guide for Nokia Devices v1.0, which is available on the Forum Nokia Web site (http://www.forum.nokia.com/java). Once the commands have been created, they should be passed to the Displayable object: Command BACK_CMD = new Command("Back", Command.BACK, 1); myScreen.addCommand(BACK_CMD); Command handler objects will be created in a way that is similar to how events and actions are handled in J2SE sometimes by inheriting from the CommandListener interface, sometimes as anonymous classes instantiated inline. These handlers will then be passed to the command the user picked: class myClass implements CommandListener { public void commandAction(Command c, Displayable s) {
Whats in MIDP 2.0: A Guide for Java Developers 12

Forum.Nokia.com

if (c == BACK_CMD) { } } } 2.4.8 MIDP 2.0 Media API

The javax.microedition.media class has been implemented as a subset of the Mobile Media API (JSR-135). The only media it supports is sound video and graphics are excluded. The MIDP 2.0 specification requires only support of tone generation. 2.4.9 Expanded connectivity

MIDP 2.0 applications are able to communicate over a range of connectivity standards. MIDP 1.0 only required support for HTTP; MIDP 2.0 adds support for secure HTTP (HTTPS). It also encourages, but does not require, support for User Datagram Protocol (UDP), straight TCP, and serial port communication. The CLDC specification provides for a Generic Connection Framework (GCF), which abstracts all forms of external data access into a single class called Connector. For example, to open a Web connection, a developer might write: Connector.open(http://www.forum.nokia.com); To open a socket: Connector.open(socket://forum.nokia.com:9001); This range of connectivity offers a wide scope for application development, from chat clients to obtaining banking information from a secure server. 2.4.10 Push architecture

In MIDP 2.0, MIDlets can be registered to wake up when a request from a registered source is made. User default phone settings and end-user settings determine whether the user is prompted before the application starts. The registration of MIDlets with the request service guards end users against receiving unwanted connections to their devices. Push, as this is called, would be particularly useful for developers of event- or turn-based games, and for in-game messaging. However, although the MIDP 2.0 specification provides support for Push, it does not require Push implementation by device manufacturers. Developers who wish to use Push must ensure that their target devices support it. 2.4.11 Timers and Alerts

Applications running under MIDP can schedule code for periodic execution using the Timer class. A single Timer object runs a background thread that can manage a list of tasks, which are run sequentially. The timer and tasking are not preemptive, which means that a process that takes longer than anticipated will cause a bottleneck with unexpected consequences. Timer tasks should be kept as short as possible. An Alert is a special Screen object that can display text and images, and will automatically terminate after a certain period of time. If the time is set to FOREVER, the device automatically provides a method of dismissing the alert. Alerts are intended for displaying error messages.

Whats in MIDP 2.0: A Guide for Java Developers

13

Forum.Nokia.com

2.4.12

Record Management System (RMS)

MIDP does not allow applications to read or write to a file system. However, RMS can be used to store persistent data such as high-score tables and saved games. The platform is responsible for making best efforts to preserve the RMS data during normal system activity, including reboots. MIDlets are allowed to create multiple record stores, as long as they have different names, and MIDlets can access record stores relating to other MIDlets within the same suite. Also, if explicit permission is given, MIDlets within other suites can also access the record store (a new feature in MIDP 2.0). The maximum size for record stores in Nokia Series 40 devices is typically 20 kB; in Series 60 devices, the only limitation is the amount of free memory. 2.4.13 End-to-end security HTTPS

As well as the encrypted networking via HTTPS, MIDP 2.0 provides added security at the MIDlet level, aimed at giving greater protection to the end user. HTTPS is secure HTTP used, for example, in Internet banking services. Securing is made by tunneling HTTP connection. Technology used for this is Transport Layer Security (TLS)/Secure Sockets Layer (SSL) over HTTP. 2.4.14 Trusted MIDlet suites

A signed MIDlet suite assures the user that the contents of the MIDlet suite have not been tampered with and that the MIDlet suite comes from an identifiable (and thus legally accountable) source. The MIDP 2.0 implementation on the device can verify the identity of the developer by verifying the developers signature.

Whats in MIDP 2.0: A Guide for Java Developers

14

Forum.Nokia.com

3 Putting It Together
3.1 What Is a MIDlet? A MIDlet is a MIDP application. A MIDP device needs to be able to control an application: Start it, stop it, and let it run at appropriate times. In practice, this means that a MIDlet is created as a subclass of the MIDlet base class so that it can be controlled by the application manager. 3.1.1 MIDlet state

A MIDlet has several possible states: 3.1.2 Active: The application is permitted to hold resources. Paused: The application must release shared resources and enter a quiet state. This is the state of the application when it is first initiated and it has not yet entered the startApp() method. Destroyed: The application must release all resources and save any persistent data. Required methods

A MIDlet must implement (at least) three basic methods to control state changes. Note that a constructor is not required, although it may be useful. void startApp() This is called to indicate to the application that it is to enter the active state. It will only be called when the application is in the paused state (which, note, is how it begins when constructed). If a failure occurs and the MIDlet cannot change state, it should call notifyDestroyed() to indicate that it should be destroyed. void pauseApp() This signals for the MIDlet to enter the paused state. The MIDlet must release shared resources and be quiescent. This method can only be called when the MIDlet is in the active state. If a run-time exception occurs during the pauseApp(), then the MIDlet will be destroyed immediately. In this case, destroyApp() will be called, allowing the MIDlet to clean up. void destroyApp(boolean unconditional) This signals for the MIDlet to terminate and enter the destroyed state. In the destroyed state, the application must release all resources and save any persistent data. This method may be called from either the Paused or the Active states. 3.2 MIDlet Structure The following example puts together the basics of MIDP 2.0. With the growing number of mobile games developers in the community, the Game API is one of the most important APIs. Therefore, it is appropriate to demonstrate a basic application to show how to use the GameCanvas. Typically, a MIDlet inherits from the MIDlet class. In this example, this MIDlet also implements the CommandListener. This will make the GameCanvas neater and more manageable, but it is a matter of choice for the developer. An effective structure is to create a set of commands, as private members that can then be registered as commands and compared with the commands sent to the

Whats in MIDP 2.0: A Guide for Java Developers

15

Forum.Nokia.com

commandAction() method. If this was a game application, a thread would be created in the GameCanvas to poll key state and act accordingly, as well as providing the animation. public class MyMidlet extends MIDlet implements CommandListener { MyGameCanvas gameCanvas; Display display; private Command exit; // the midlet constructor public MyMidlet() { try { gameCanvas=new MyGameCanvas(); Display.getDisplay(this).setCurrent(gameCanvas); //create the commands exit=new Command("Exit", Command.EXIT,1); gameCanvas.addCommand(exit); gameCanvas.setCommandListener(this); } catch(IOException e) { System.out.print(e.toString()); } } public void startApp() throws MIDletStateChangeException { . . . } public void pauseApp() { . . . } public void destroyApp(boolean unconditional) { . . . } public void commandAction(Command command, Displayable display) { if(command==exit) { destroyApp(true); notifyDestroyed(); } } } The MIDlet constructor instantiates the GameCanvas object. The GameCanvas creates a Sprite, which is based on an image stored in the resource directory of the MIDlet suite. This Sprite is then appended to the layer manager, which paints the Sprite to the screen.

public class MyGameCanvas extends GameCanvas { private MySprite sprite; private LayerManager layerManager; public MyGameCanvas() throws IOException {
Whats in MIDP 2.0: A Guide for Java Developers 16

Forum.Nokia.com

super(true); sprite=createSprite(); // initialize the layer manager layerManager=new LayerManager(); // append the sprite to the layer manager layerManager.append(sprite); Graphics graphics = getGraphics(); draw(graphics); } private void draw(Graphics g) { // calculate the center of the screen based upon // the images and canvas size int x= (getWidth()/2-sprite.getWidth()/2); int y= (getHeight()/2-sprite.getHeight()/2); // set and draw the background g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight()); // paint the sprite on the screen layerManager.paint(g,x,y); } private MySprite createSprite() throws IOException { Image image = Image.createImage("/nokia_logo.png"); int width=image.getWidth(); int height=image.getHeight(); return new MySprite(image, width, height); } } The Sprite object in this application is a simple class, which is purely designed to store the image within a Sprite. public class MySprite extends Sprite { public MySprite(Image image, int width, int height) { super(image, width, height); } } 3.3 3.3.1 Deploying Your MIDlet Compiling

MIDlets are compiled using the standard Java SDK. Of course, there are many tools available to aid developers. Developers can use their favorite IDE, or work with the KToolBar application, which comes with the J2ME Wireless Toolkit. Nokia Developers Suite for J2ME, Version 2.0, doesnt include a compiler, but provides a lot of useful tools for creating new classes and MIDlet suites, deploying MIDlets to devices, and so on. 3.3.2 Preverifying

As discussed in Section 2.3.2, Class loader, the class loader for MIDP devices is very limited. Therefore, it is necessary to preverify the .class files before they are packaged. This ensures the classes are compatible with the J2ME virtual machine. The tools included with the J2ME Wireless Toolkit or the Nokia Developers Suite will handle the preverify step.

Whats in MIDP 2.0: A Guide for Java Developers

17

Forum.Nokia.com

3.3.3

Emulating

The J2ME Wireless Toolkit and the Nokia Developers Suite also include emulators for a variety of devices. Developers can test and debug MIDlets on the emulators until they are satisfied with their MIDlets basic operation. Emulators also give the developer the ability to see how the user interface will operate in different devices. Nokia provides two types of emulators: ones supplied with concept SDKs, and ones supplied with SDKs using real phone software. The latter more closely emulate the target devices. However, it must be remembered that there is no substitute for testing on real target devices, because PCs are different from mobile phones, both in technical capability and in UI (mouse and keyboard vs. phone keypad). 3.3.4 Packaging

In order to deploy a MIDlet to a real device, it must be packaged into a suite. A suite is a compilation of one or more MIDlets into a collection of related classes and resources such as graphics, sound files, and RMS data. The package helps to save space on the device. The Nokia Developers Suite and the J2ME Wireless Toolkit both provide the functionality to create this package. The Nokia Developers Suite provides the capability to deploy the suite to a Nokia phone. 3.3.5 Over-the-Air provisioning

During the course of development, MIDlet suites can be deployed in a variety of ways; however, commercially it is usually best to deploy applications Over the Air (OTA). With MIDP 2.0, compliant devices are now required to implement and support OTA deployment. In MIDP 2.0-compliant devices it is possible to use MIDlet-Install-Notify and MIDlet-Delete-Notify attributes to return a code to the deployment server whenever a MIDP 2.0 MIDlet is installed or deleted. This allows developers and publishers to more easily track usage of MIDlets. For information on configuring a server to deploy MIDlets over the air to Nokia devices, see the document Settings for OTA Download of MIDlets at the Forum Nokia Web site (it can be found on the Game Programmers page, accessible through http://www.forum.nokia.com/games).

Whats in MIDP 2.0: A Guide for Java Developers

18

Forum.Nokia.com

4 Conclusion
This completes the whirlwind tour of MIDP 2.0. Developers should now be familiar with the capabilities of the MIDP environment, what can and cannot be done, the basic structure of a MIDlet, and a brief outline of some of MIDPs more useful features.

Whats in MIDP 2.0: A Guide for Java Developers

19

Forum.Nokia.com

5 References
5.1 Sun Documents

Whats New in MIDP 2.0, http://java.sun.com/products/midp/whatsnew.html#enhanced The Java 2 Platform, Micro Edition datasheet, http://java.sun.com/j2me/j2me-ds.pdf
JSR-118, Mobile Information Device Profile 2.0, Final Release, http://jcp.org/aboutJava/communityprocess/final/jsr118/index.html 5.2 Tools J2ME Wireless Toolkit, http://java.sun.com/j2me/download.html Nokia Developers Suite for J2ME, Version 2.0, http://www.forum.nokia.com/tools Borland JBuilder, http://www.Borland.com/jbuilder Sun ONE Studio, http://www.sun.com/software/sundev/jde 5.3 Nokia Documents These are all available on the Game Programmers page, which may be accessed via a link on the main Forum Nokia Games page at http://www.forum.nokia.com/games. (In some cases, developers may have to follow the link on that page to the Game Programmers page, as only the most recent documents appear on the main games page.)

Whats in MIDP 1.0: A Guide for Java Developers Java MIDP Application Developers Guide for Nokia Devices Settings for OTA Download of MIDlets

Whats in MIDP 2.0: A Guide for Java Developers

20

Forum.Nokia.com

6 Terms and Abbreviations


Term or Abbreviation
CLDC Configuration J2ME

Meaning
Connected Limited Device Configuration. The particular configuration within which MIDP is defined. The minimum set of class libraries available for a range of devices. Java 2 Platform, Micro Edition. The version of Java technology designed for small devices such as mobile phones and other mobile devices. A limited virtual machine intended for implementation on small devices. It is defined as part of the CLDC. An application designed for MIDP. Mobile Information Device Profile. The standard profile currently used for mobile devices. The specific set of class libraries that must be available to a particular range of devices. Record Management System. A mechanism available within MIDP for providing a limited amount of persistent storage.

KVM MIDlet MIDP Profile RMS

Whats in MIDP 2.0: A Guide for Java Developers

21

Forum.Nokia.com

Build > Test > Sell


Developing and marketing mobile applications and services with Nokia

Get started
Use free resources available through http://www.forum.nokia.com/: articles covering the latest technical and business issues, tools for developing and deploying applications and services, biweekly newsletters, and active discussion boards. http://www.forum.nokia.com

Download tools and SDKs


Download free SDKs, emulators, simulators, and other tools that integrate with industry-leading integrated development environments. http://www.forum.nokia.com/tools

Get specifications and documentation


Get comprehensive device and platform specifications, and find detailed technical documentation, tutorials, technical notes, case studies, FAQs, and more. http://www.forum.nokia.com/devices http://www.forum.nokia.com/documents

Get support and testing services


Access Nokias wide range of technical support services, including case resolutions from our professional support staff and fee-based online support from our experts. Forum Nokias Developer Hub services help with development and testing by providing both online and onsite access to servers, messaging centers, and the like. http://www.forum.nokia.com/support

Take your applications to market


Take your applications and services to market through Nokia Tradepoint and Nokia Software Market. Other opportunities are available through Nokia Mobile Phones and other Series 60 Platform licensees. http://www.forum.nokia.com/business

Build your business with Nokia


Nokia works with selected leading developers in the games, branded media and content, and enterprise software industries. Submit your application to Nokia at http://www.forum.nokia.com/business. http://www.forum.nokia.com/business

Whats in MIDP 2.0: A Guide for Java Developers

22

Vous aimerez peut-être aussi