Developing Java ME Applications for Multiple Devices
This tutorial is the second in series designed to get developers started with MIDP and CDC development. It is strongly recommended
that you complete the Java ME MIDP Quick Start Guide before beginning this tutorial. We continue building
on the "MyHello" project completed in the MIDP Quick Start and show you how to deploy it to different emulators and customize the code
for specific device environments. Note that the concepts and methods demonstrated in this tutorial can also be applied to CDC applications created in the IDE.
Contents
Requirements
To complete this tutorial, you need the following software and resources:
Adding Emulator Platforms
Emulator platforms are provided by mobile phone manufacturers so that developers
can simulate the behavior of specific mobile devices to help debug the applications they create. NetBeans IDE Mobility includes the Sun Java Wireless
Toolkit, and supports many other software development kits (SDKs) that you can
download from vendor sites such as Nokia, Sony Ericsson, and Motorola. You can check the NetBeans Mobility FAQ for a list of supported MIDP emulators that includes download links.
Adding a New Emulator Platform
- Choose Tools > Java Platforms from the main toolbar.
- In the Java Platform Manager dialog, click the Add Platform button.
- In the Add Java Platform dialog, select the Java ME MIDP Platform Emulator. Click Next.
- In the Add Java Platform dialog, the IDE searches for all available Java ME platforms
and lists them in the Platform Folders section. Platforms that are compliant with the Unified
Emulator Interface (UEI) standard are verified and appear with a check in
the checkbox next to the platform name. Platforms that are not UEI-compliant,
or that require customization to install are listed in
red. The wizard also identifies which platforms have already been installed.
NOTE: To install platforms that are not UEI-compliant, click the Back button and choose Custom Java Micro Edition Platform Emulator.
- In the Platform Folders section of the Add Java Platform dialog, check all of the platform emulators you want
to install. After the platforms are detected, they are installed. Click
Finish. Then click Close.
- To add a new emulator platform to a project Choose File > "MyHello" Properties.
- Choose the Platform node, then choose the name of the new emulator (for
example, Sun Java Wireless Toolkit 2.5.2) from the Emulator Platform drop-down
menu. Click OK.
Changing the Default Emulator Device
- Right-click the MyHello Project node and choose Properties. In the Properties
dialog, choose the Platform node. Here, you can change the device for the
default configuration.
- Click the Device drop-down list and choose QwertyDevice. Click OK.
- Run the application again, and the application runs in the
QwertyDevice
emulator.
Using Configurations
You can use configurations to create more than one set of distribution JAR
and Java Application Descriptor (JAD) files for your project. This enables you
to create one set of source code and customize it for each mobile device you're
programming for.
Adding a Configuration
- Right-click the MyHello Project node and choose Properties. In the Properties
dialog, Choose Add Configuration from the Project Configuration drop-down
list. This opens the Add Configuration dialog.
- Enter a name for your configuration in the Configuration Name field. The
name should identify the target platform you deploy the JAR/JAD
created for this configuration. For this example, enter
BlackWhiteDisplay
and click OK. You have just created a new configuration.
- Configurations can also be added by clicking the Manage Configurations
button in the Properties dialog. This brings up the Project Configuration
Manager dialog which contains options for adding, removing, or duplicating
configurations.
Changing the Project Configuration
You can create different types of project configurations using the templates in the New Project wizard.
You can also use the drop-down Configuration menu in the main toolbar to quickly change project configurations.
Customizing a Configuration
You can add as many configurations as you would like to your project. You
can then modify settings in the Project Properties dialog for each configuration
that you've added.
- Right-click the MyHello Project node and choose Properties. In the Properties
dialog, choose the Platform node.
- Choose the
BlackWhiteDisplay configuration from the configuration
combo box at the top of the Properties dialog.
- The components in the Platform dialog are all disabled. This is because
this configuration is currently taking the values used by the default configuration
for this panel. Uncheck the Use Values from "DefaultConfiguration"
option at the top of the panel. All components on this panel can now be
edited.
- Change the Device option to DefaultGrayPhone.
- Toggle the Project Configuration dialog at the top of the Properties dialog
and observe that the Device option changes based on which configuration
is currently selected. This method of configuration customization works
for each panel (other than the general panel) in the dialog. Click OK to
save your configuration changes and exit the Properties dialog.
Creating an Ability
An Ability is a specific attribute of a project configuration. It
can be a physical attribute such as screen size, or an API or JSR supported
by the configuration. You can create a new ability and associate it with one
or more project configurations. You can then use preprocessior code blocks
to associate certain code with that ability. If you later need to add or remove
configurations, you do not have to go through all your code and associate the
configuration with each code block. Instead, you need only to add that ability
to the configuration. The configuration is then automatically associated with
any code block associated with that ability.
- Right-click on the MyHello project node and choose Properties.
- Choose Abilities from the Category menu tree.
- Choose a configuration from the Project Configuration dropdown menu. This
associates the ability with that configuration.
- If necessary, uncheck the Use Values from "DefaultConfiguration" checkbox.
- Click the Add button.
This opens the Add Ability dialog.
- In the Add Ability dialog, enter a name for the ability, or choose an
ability from the drop-down menu. Optionally, enter a value for the ability.
Click Ok to close the Add Ability dialog. The ability is now associated
with the selected project configuration.
You can associate the ability with other configurations by choosing a different
configuration from the Project Configuration menu and clicking the Add button.
Adding Preprocessor Code for Specific Configurations
Preprocessor blocks enable you to create, manage and track code that is specific
to one or more project configurations or abilities. The code is enabled (or
disabled) only for the configurations or abilities you identify.
- In the HelloMIDlet.java Editor window, right-click on the source code
line where TextBox is instantiated (
helloTextBox = new javax.microedition.lcdui.TextBox(null,
"Make my day",120, 0x0);).
- Right-click on the line and choose Preprocessor Blocks > Create If/Else
Block. In the first, pink section, append the word "Color" to the "Hello
World" string. In the second, gray section, add "Gray" to the "Make my day "
string.
- The code marked by the
//#if preprocessor directive is in effect whenever the active configuration matches the directive value
(in this case, DefaultConfiguration.) The code encased by the //#else
preprocessor directive is used whenever the active configuration does
not match the value of the preprocessor.
In other words, the emulator displays "Make my day Color" whenever
the active platform is "DefaultConfiguration," and displays "Hello
World Gray" if the active platform is any other emulator platform.
The //#endif preprocessor directive completes the preprocessor block.
You can test the preprocessor code by running the MIDlet using different
emulator devices, as described in Changing the
Emulator Platform.
Running Configurations
Configurations can be built and run individually or simultaneously.
- Right-click the MyHelloMIDlet Project node and choose Run.
- Use the configuration combo box in the Toolbar to change the configuration
to BlackWhiteDisplay. Choose Run again.
- Two emulators appear, one color and one gray. The gray emulator displays
"Make my day Gray" and the color emulator displays "Make my day Color."
-
To build multiple configurations simultaneously, right-click
on a project and choose Batch Build. In the dialog that opens, check the
box next to each configuration you want to build.
-
To test a device on the fly, right-click on the project and choose Run
With. In the dialog that opens, choose the emulator platform and device you
want to run.
See Also
So far we've created a simple application, launched it on multiple emulators and added code customization to ensure that it deploys properly on multiple devices.
The next step in getting started with MIDP and CLDC development is learning additional techniques for resolving device fragmentation issues.