Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Skill Level: Introductory Michael Juntao Yuan Author, Analyst, and Developer
18 Jan 2005 This tutorial shows how to develop J2ME applications for Nokia handsets using the Eclipse IDE and the Nokia Developer's Suite for J2ME.
Prerequisites
To complete the tasks and run the sample code in this tutorial, you'll need to have the Eclipse IDE Version 2.1.x or 3.x installed. You'll also need to register a free user
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 1 of 43
developerWorks
ibm.com/developerWorks
account at Forum Nokia (http://www.forum.nokia.com/). I cover how to download and install the Nokia Developer's Suite for J2ME in the tutorial.
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 2 of 43
ibm.com/developerWorks
developerWorks
As the world's largest mobile phone manufacturer, Nokia produces dozens of handset models at any given time. For developers, it can be a very confusing landscape. It takes a lot of time to research the characteristics and programming environment available on each device. To make life easier for developers, Nokia designs its handsets around several developer platforms. Each developer platform contains devices that have similar physical characteristics (such as screen size and memory size), UI styles, lead software, and programming environments. The Nokia Series 40 Developer Platform is for mass market devices such as the Nokia 7210 and 6230. It has by far the largest market share with 100 million Series 40 devices sold every year. The Nokia Series 60 Developer Platform is for Symbian-OS-based smartphone devices such as the Nokia 3650 and 6600. More than 10 million Series 60 devices are sold every year The Nokia Series 80 Developer Platform is for Symbian-OS-based high-end enterprise devices such as the Nokia 9500 and 9300 Communicators. The Nokia Series 90 Developer Platform is for Symbian-OS-based touch-screen multimedia devices such as the Nokia 7700 prototype device. There is currently no commercial device offered that uses the Series 90 platform. J2ME is supported on all Nokia developer platforms. The 2.0 version of the Series 40, 60, 80 and 90 platforms all support MIDP 2, the Wireless Messaging API, and the Mobile Multimedia API. They also provide optional support for the Bluetooth API, the Mobile 3D API, and the File I/O and PIM API.
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 3 of 43
developerWorks
ibm.com/developerWorks
Starting Eclipse
Now, start the Eclipse IDE and you see a new Tools > Nokia Developer's Suite for J2ME menu item as well as a number of new icons on the tool bar. Figure 1. New menu in Eclipse
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 4 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 5 of 43
developerWorks
ibm.com/developerWorks
1. 2.
Select Tools > Nokia Developer's Suite for J2ME > Configure Emulators. Click Add to add new SDKs to the drop-down list. Figure 2. Add new SDKs
3.
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
ibm.com/developerWorks
developerWorks
Configuring emulators
To configure the behavior of the device emulator: 1. 2. 3. Select Tools > Nokia Developer's Suite for J2ME > Configure emulators. Choose the emulator SDK from the drop-down list. Click Preference. You are able to set general properties of the emulator.
The Phone Number prefix field specifies the artificial phone number assigned to the emulator when the phone network environment is simulated. For example, the first instance of the emulator is assigned phone number "6230000" and the second instance is assigned "6230001". The two emulator instances can send SMS/MMS messages to each other using the phone numbers. Figure 4. General properties
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 7 of 43
developerWorks
ibm.com/developerWorks
The MIDP run-time properties such as the heap size and security domains are configurable too. Figure 5. Configure MIDP
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 8 of 43
ibm.com/developerWorks
developerWorks
The logging level of emulator can be specified in the MIDP Monitor tab. Here, you can also slow down the emulator's execution speed to emulate the slow CPU on devices on a fast PC. Figure 6. Logging level
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved. Trademarks Page 9 of 43
developerWorks
ibm.com/developerWorks
You have installed the tools. Let's go through the entire process of building, testing, and deploying an MIDP application using those tools.
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 10 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 11 of 43
developerWorks
ibm.com/developerWorks
3.
Specify a name and a root directory for the project. Figure 8. Create project name
4.
Choose an SDK to develop against if multiple SDKs are available. Figure 9. Select an SDK
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 12 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 13 of 43
developerWorks
ibm.com/developerWorks
By default, the project's classpath includes the J2ME API libraries in the SDK. You can add any external libraries into the classpath. Warning: If you do add external Java Archive (JAR) libraries that are not available on the physical device to the project, you must package them into the distribution JAR file manually. Otherwise, the MIDlet throws a "Class not found" exception when you deploy it on the device. Figure 11. Define the classpath
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 14 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 15 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 16 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 17 of 43
developerWorks
ibm.com/developerWorks
The skeleton Java source code file generated from the wizard is as follows. Figure 14. Generated source code
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 18 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 19 of 43
developerWorks
ibm.com/developerWorks
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class TutorialMidlet extends MIDlet implements CommandListener { Display display; Command greetCommand; Command exitCommand; Command clearCommand; Command backCommand; WelcomeScreen welcomeScreen; HelloScreen helloScreen; // instantiate the internal variables public TutorialMidlet () { display = Display.getDisplay(this); greetCommand = new Command ("Greet", Command.OK, 0); exitCommand = new Command ("Exit", Command.EXIT, 0); clearCommand = new Command ("Clear", Command.CANCEL, 1); backCommand = new Command ("Back", Command.SCREEN, 1); welcomeScreen = new WelcomeScreen (); welcomeScreen.addCommand (greetCommand); welcomeScreen.addCommand (clearCommand); welcomeScreen.setCommandListener (this); helloScreen = new HelloScreen (); helloScreen.addCommand (exitCommand); helloScreen.addCommand (backCommand); helloScreen.setCommandListener (this); } // Called when the MIDlet is started by the AMS protected void startApp () { display.setCurrent (welcomeScreen); } protected void pauseApp () { // Do nothing } protected void destroyApp (boolean unconditional) { notifyDestroyed (); } public void commandAction (Command c, Displayable d) { if (c == greetCommand) { String name = welcomeScreen.getName (); helloScreen.setName(name); display.setCurrent (helloScreen); } else if (c == clearCommand) { welcomeScreen.setName(""); display.setCurrent(welcomeScreen); } else if (c == backCommand) { display.setCurrent (welcomeScreen); } else if (c == exitCommand) { destroyApp (true); } } }
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 20 of 43
ibm.com/developerWorks
developerWorks
If the Build automatically option in the Eclipse Project menu is selected, Eclipse tries to continuously build the project in the background whenever the project is
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved. Trademarks Page 21 of 43
developerWorks
ibm.com/developerWorks
updated. In this case, the Package Explorer also shows the compiling errors detected in the building process. Figure 16. Automatic build errors
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 22 of 43
ibm.com/developerWorks
developerWorks
import javax.microedition.lcdui.*; public class WelcomeScreen extends Form { private TextField nameField; public WelcomeScreen () { super ("Welcome"); Image img; // Construct the image from the media file try { img = Image.createImage("/welcome.png"); } catch (Exception e) { e.printStackTrace (); img = null; } ImageItem imageItem = new ImageItem ("", img, ImageItem.LAYOUT_CENTER, "Welcome"); nameField = new TextField ("Please enter your name", "", 10, TextField.ANY); append (imageItem); append (nameField); } public void setName (String n) { nameField.setString (n); } public String getName () { return nameField.getString (); } }
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 23 of 43
developerWorks
ibm.com/developerWorks
height = getHeight (); name = "unknown"; // Construct the image from the media file try { img = Image.createImage("/hello.png"); } catch (Exception e) { e.printStackTrace (); img = null; } } public void setName (String n) { name = n; } // Paint the screen based on the name protected void paint (Graphics g) { g.setColor(0xffffff); g.fillRect(0, 0, width, height); g.setColor(0x000000); g.drawImage (img, width / 2, height / 4, Graphics.VCENTER | Graphics.HCENTER); g.setFont(Font.getFont( Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE)); g.drawString (name, width / 2, height * 3/4, Graphics.BASELINE | Graphics.HCENTER); } }
No more errors
With all the source code typed in, Eclipse shows no errors or inconsistency. You are ready to run the midlet! Figure 17. No errors; run the midlet
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 24 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 25 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 26 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 27 of 43
developerWorks
ibm.com/developerWorks
The emulator responds to user input. Figure 21. Emulator responds to user input
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 28 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 29 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 30 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 31 of 43
developerWorks
ibm.com/developerWorks
emulator starts normally, but when you hit the break point (such as pressing a soft key to invoke the command handler commandAction() method in this case), Eclipse changes its display from the Java perspective to the debugging perspective. It displays the call stack, the current break point, and values of variables in the stack. After reviewing those data, you can resume execution using the Run. Figure 24. The debugging perspective
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 33 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 34 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 35 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 36 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 37 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 38 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 39 of 43
developerWorks
ibm.com/developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 40 of 43
ibm.com/developerWorks
developerWorks
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 41 of 43
developerWorks
ibm.com/developerWorks
Downloads
Description
Code sample Information about download methods
Name
wi-nokiaeclsource.zip
Size
39 KB
Download method
HTTP
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 42 of 43
ibm.com/developerWorks
developerWorks
Resources
Learn Stay current with developerWorks technical events and Webcasts. The J2ME 101 series of tutorials on IBM developerWorks is a good place to get started with J2ME and MIDP programming. The IBM WebSphere Studio Device Developer is another Eclipse-based commercial IDE that targets J2ME developers. Get products and technologies Build your next development project with IBM trial software, available for download directly from developerWorks. You can download the Eclipse IDE from the Eclipse web site. The web site also has documentation on how to use the IDE and how to develop plug-ins. Forum Nokia's tools section has the Nokia Developer's Suite 2.2 for J2ME and related device SDKs for free download. Discuss Participate in developerWorks blogs and get involved in the developerWorks community.
Use Eclipse to develop mobile applications for Nokia devices Copyright IBM Corporation 2005. All rights reserved.
Trademarks Page 43 of 43