Slater, S. (2007) JAVA based MIDP 2.0 tutorials: Tutorial 1

advertisement
Complementary Mobility
Introduction to Mobile Application Development
Table of Contents
Introduction .............................................................................................................................................. 2
Pre-Installation Information ..................................................................................................................... 4
Obtaining the wireless toolkit ................................................................................................................... 4
Development Notes .................................................................................................................................. 4
Installing the Toolkit ................................................................................................................................. 5
Using the Toolkit ....................................................................................................................................... 8
The Project Development Life Cycle ....................................................................................................... 10
Testing a Midlet using the emulator ....................................................................................................... 12
1: Introduction to Mobile Application Development
Page: 1
Complementary Mobility
Introduction to Mobile Application Development
Introduction
This introduction discusses thick applications using J2ME which is Java applications running on mobile
devices. Standard Java is too large to run on a mobile device and mobile devices have unique traits from
typical desktop computers. With this in mind a subset of Java libraries is available specifically designed
for development of applications for mobile devices – these libraries are collectively called J2ME and are
used to design Midlets.
This introduction assumes you are using Windows XP as your host PC operating system.
Articles in this series include:





1: Introduction to Mobile Application Development
2: Beginning application development with MIDP
3: Transferring an application Midlet to a mobile device
4: Building an interactive application Midlet with MIDP
5: Building a multimedia application with MIDP
For developing applications on mobile devices three methods exists, these are native applications, thin
client applications and thick client applications.
1: Introduction to Mobile Application Development
Page: 2
Complementary Mobility
Introduction to Mobile Application Development
Mobile Information Device Profile (MIDP) combined with Connected Limited Device Configuration
(CLDC) based applications are small footprint programs that run in Java and are commonly known as
Midlets. CLDC contains subsets of J2SE routines that are optimised for mobile devices, including
java.lang, java.io, and java.util. MIDP libraries include java.microedition.lcdui, java.microedition.rms, and
java.microedition.midlet.
MIDP specifices a number of libraries called API (Application Programming Interface) for mobile
development including routines for network connectivity, user interfaces and local data persistence. The
routines found in J2ME are typically subsets of routines found in J2SE but designed with mobile
applications in mind. These APIs make it easier to develop applications for mobile devices as they mean
that low-level device knowledge is not required.
Although Midlets may be intended to run on a mobile device such as a mobile phone or a personal
digital assistants (PDA) they are usually developed and tested using a personal computer (PC).
Development requires the Java Software Development Kit (Java SDK), a toolkit based on the MIDP
specification, as well as a method for editing programs. Program editors can range from a complete
integrated development environment (IDE) or a simple text editor such as Notepad.
1: Introduction to Mobile Application Development
Page: 3
Complementary Mobility
Introduction to Mobile Application Development
Pre-Installation Information
This introduction assumes you already have a Java SDK installed and configured and that you are using
Notepad for editing programs. This guide uses the Sun Java Wireless Toolkit, of which it’s minimum
hardware requirements are:



100 MB hard disk space
128 MB system RAM
800 MHz Pentium III CPU
You must ensure you have Java SDK installed on your machine. You can check this by running a
command window and typing:
java –version
Obtaining the wireless toolkit
As at October 2007 you can download the Sun Java Wireless Toolkit from:
http://java.sun.com/products/sjwtoolkit/
Development Notes






The Sun Java Wireless Toolkit can be downloaded from:
http://java.sun.com/products/sjwtoolkit/
Class files should always be pre-verified prior to deployment
Midlet debugging using the emulator can use System.out.println() to output data to the console
Source code must have the .java extension and be in the source folder under the project
Add a PATH to midp\bin to allow applications to be emulated from the command prompt
Command line compilation must use the –bootclasspath option to tell the compiler to use MIDP
classes in preference to the default ones
1: Introduction to Mobile Application Development
Page: 4
Complementary Mobility
Introduction to Mobile Application Development
Installing the Toolkit
Once the Wireless toolkit is downloaded you can run it where you will be met with the 'starting
InstallShield Wizard' window.
From here, click ‘Next’ to start the installation
process.
Then Read and confirm the License Agreement by
clicking on Accept.
Either choose a folder to install the Java Virtual
Machine into or accept the default and click Next.
1: Introduction to Mobile Application Development
Page: 5
Complementary Mobility
Introduction to Mobile Application Development
Either choose a folder to install the Wireless Toolit
into or accept the default and click Next.
Select a program folder or accept the default and
press Next.
You now have the opportunity to check for the
latest program updates. You can do this by
enabling the checkbox and clicking Next.
If you do not wish to check for product updates at
this time then leave the box blank and click Next.
1: Introduction to Mobile Application Development
Page: 6
Complementary Mobility
Introduction to Mobile Application Development
Now ensure you are happy with the settings and
click Next.
The program will go through it’s installation
procedure. The progress indicator will guide you.
Upon completion click Finish.
1: Introduction to Mobile Application Development
Page: 7
Complementary Mobility
Introduction to Mobile Application Development
The first time you run the program you may
receive this warning box. Make sure you click
Unblock to tell the firewall to allow this program
to run using Internet access.
Using the Toolkit
On Microsoft Windows choose Start > Programs > Sun Java Wireless Toolkit 2.5.2 for CLDC > Wireless
Toolkit 2.5.2
To start the program click on the icon where you
will be met with the following splash screen.
You will be presented with the following screen.
To begin a new project click on the New Project
button.
Enter a Project Name and Midlet Class name.
You can set additional options on this screen
alternatively you can simply accept the given
defaults.
1: Introduction to Mobile Application Development
Page: 8
Complementary Mobility
Introduction to Mobile Application Development
When developing applications using the Wireless Toolkit your development projects, using the default
install options, are stored as subfolders under the project within the apps directory:
Wireless Toolkit installation folder
\apps
\ProjectName projectname is based on the project name you specified earlier
\bin
\lib
\res
\src
you should save your project source code in this folder
1: Introduction to Mobile Application Development
Page: 9
Complementary Mobility
Introduction to Mobile Application Development
The Project Development Life Cycle
When you are ready to start Midlet development you can use the project development lifecycle, as
shown in the diagram, to assist you. This involves editing the source code, compiling, pre-verifying then
packaging your application, then testing and deploying it. To save time and ease development your
application can be tested using a built-in mobile device emulator.
Figure 1: The Build Cycle
The design process is an important aspect of development as devices that run Midlets often have limited
memory capabilities. A Midlet that works on one device may not work on another device if it has less
memory, this a major consideration that will influence the design process.
At the time of Compilation it is important to tell the compiler to over-ride its built in libraries and use the
MIDP libraries instead. This is often achieved on the command line by using the –bootclasspath
parameter, e.g:
Javac –bootclasspath C:\WTK2.5.2\lib\cldca10.jar;c:\WTK2.5.2\lib\midpapi20.jar myApp.java
Unless this parameter is used, during a command line compilation, the compiler will use the standard
Java SDK libraries instead of the MIDP specific libraries. Pre-verification checks the compiled files to
ensure they are suitable for packaging up for deployment to a mobile device, this is because resources
are limited on a mobile device so it is necessary to ensure that the developed Class files will function in
the Java Virtual Machine (JVM). Preverification also splits the byte-code verification that is found in the
Java runtime security model into two steps. The byte-code verifier is too large for many mobile
1: Introduction to Mobile Application Development
Page: 10
Complementary Mobility
Introduction to Mobile Application Development
applications so the Class files during pre-verification are manipulated to run with a small verifier.
Fortunately, the wireless toolkit automates the build and pre-verify phases which can ease
development.
Packaging follows which puts all the files into a single .jar file. This will need to include the code, any
images, and the manifest file. The manifest file is often used by mobile devices and contains a
description of what is contained within the .jar file.
On certain devices just the .jar file is required but on other devices you may need to copy the .jad file
too. Sometimes a .jar file can become very large so a .jad file contains a description of the files
contained within the associated .jar file which becomes a much smaller file. However, a .jad file cannot
be used without a .jar file. An example of the contents of a .jad file is
MIDlet-1: myApp, myApp.png, myApp
MIDlet-Jar-Size: 1387
MIDlet-Jar-URL: myApp.jar
MIDlet-Name: myApp
MIDlet-Vendor: Unknown
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
1: Introduction to Mobile Application Development
Page: 11
Complementary Mobility
Introduction to Mobile Application Development
Testing a Midlet using the emulator
Testing an application is one of the most important stages of development, however it can become even
more important with mobile applications when your application may run on a variety of different
devices. Spending time prior to development on analysis and design will help the testing process and
your skills as a developer will also play an important part. Having access to as many devices as you can
get hold of that could run your application is somewhat impractical which is where an emulator will
come in handy. An emulator replicates the functioning of a physical device in software.
As previously mentioned the Sun Wireless Toolkit includes a number of built-in emulators that can be
used to test your applications prior to deployment, these include 240x320 colour display, a 108x240
grey display, a smartphone/thumbphone emulator with a 636x235 colour display, and a 108x208 colour
display with media keys. The application should be pre-verified before testing to ensure the correct
libraries are included. The toolkit, such as that shown in the diagram, can emulate a number of devices
from simple mobile phones to more advanced devices. You can either launch the emulator from the SDK
window or you could modify your environment PATH variable to include the \midp\bin folder which will
allow you to start an application in an emulator using the following command (from a command
window):
midp ProjectName
Alternatively, simply use the default device (or pull down the Device option list to choose a custom
device) then select Run from the menu to start the application in the emulator.
1: Introduction to Mobile Application Development
Page: 12
Complementary Mobility
Introduction to Mobile Application Development
To interact with the onscreen device emulator you can click the appropriate buttons as you would
expect to press them on a real device. The two soft buttons shown in the image are configurable. When
finished testing your application either exit or simply close the emulator window.
1: Introduction to Mobile Application Development
Page: 13
Download