|
Everyone is talking about the Wireless Internet, and that means we will all soon be able to surf the 'net with our palm pilots, cell phones, and other mobile devices. Currently, there is a lot of interest in creating applications for this market, and currently there are a few ways to do it. The Wireless Application Protocol (WAP) is one way, and another is to use Sun Microsystems's Java 2 Micro Edition (J2ME) Mobile Information Device Profile (MIDP). This article is part of a series on wireless applications programming using the MID Profile APIs. It introduces you to the basics of MIDP programming and explains how to package MIDP applications (also called MIDlets). Please note that this article does not describe how to set up your MIDP environment. For setup information for the J2ME Wireless Toolkit software, see Quick Guide to the J2ME Wireless Tookit. If you prefer using the MIDP and CLDC seperate downloads with system configurations, see MIDP Setup Explained. For an overview of MIDP, see Introduction to Wireless Programming with the MID Profile. For a tutorial on WAP, see WAP for Java Developers. Introduction Most Internet technologies are designed for desktop and large computers running on reliable networks with relatively high bandwidth. Hand-held wireless devices, on the other hand, have a more constrained computing environment. They tend to have less memory, less powerful CPUs, different input devices, and smaller displays. Further, wireless networks have less bandwidth and more latency (delay) compared to wired computer networks. The Wireless Application Protocol (or WAP), which is a specification developed by the WAP Forum, takes advantage of the several data-handling approaches already in use. Developing wireless applications using WAP technologies is similar to developing web pages with a markup language because it is browser based. Another approach to developing wireless applications is to use the Mobile Information Device Profile (MIDP). With either WAP or MIDP, the Java programming language plays an important role. In WAP, Java Servlets and Java Server Pages (JSPs) can be used to generate Wireless Markup Language (WML) pages dynamically, and in MIDP, applications (also called MIDlets) are written in the Java programing language. The rest of this article focuses on using the Java programing language to write MIDlets. Look and Feel
To get started with MIDP programming, install the MIDP reference implementation as described in MIDP Setup Explained. Once you have your environment set up, you might want to test it.
MIDP comes with several demo programs. To try the demos, run the c:> midp -descriptor run.jad If everything goes well, you see something similar to Figure 1 at left.
Because some mobile phones have color displays, MIDP supports several color ranges,
so you can run applications in color. To run applications in color, set the
c:> set SCREEN_DEPTH=8 On Unix, setting the screen depth depends on the shell you are using so you might want to consult your documentation. Once you have set the screen depth to 8, you can run the demo application in color using the same command shown above and repeated here: c:> midp -descriptor run.jad You should now see the demo application in color as shown in Figure 2 at right. The MIDP Development Life Cycle MIDP applications (or MIDlets) can be easily developed by following the few steps outlined here:.
Developing MIDlets is not only easy, but it is also fun and interesting. MIDlet programming is easier than J2SE programming because the MIDP API is simpler. You only need to learn about a few classes before you can start writing your own MIDlets. Now, let's look at the genesis of a simple MIDlet by developing one. The MIDlet we will develop displays some text and multiple commands. Step 1. Writing the Application Our MIDlet, when launched, displays a simple text message and a few commands. The source code for this MIDlet is shown in Listing 1.
Note the following from Listing 1:
In
As you can see, a command contains three pieces of information: a label, a type, and a priority. The label (which is a string) is used for the visual representation of the command. The type of the command specifies its intent. And the priority value describes the importance of this command relative to other commands on the screen. A priority value of 1 indicates the most important command, and higher priority values indicate commands of lesser importance. When the application is executed, the device chooses the placement of a command based on the type of the command, and places similar commands based on their priorities. In the above example, there are the following three commands:
In this example, the application manager maps the
Figure 3.1: First Launched Figure 3.1 shows how the screen looks when the application is first launched.
Figure 3.2: Exit & Menu Commands Clicking the selection button takes you to Figure 3.2.
Figure 3.3: Info & Buy Commands
And clicking the soft button under Step 2: Compile and preverify
Compiling an MIDP class is performed, using the c:> javac -d .\ -bootclasspath c:\pathTo\MIDP\classes FirstMIDlet.java
This command produces a
After compiling your program, you must process it with the c:>preverify -classpath c:\pathTo\MIDP\classes; c:>pathTO\FirstMIDlet.class FirstMIDlet.class
The above command creates the
Step 3: Testing the application
Testing the application can be done easily with the c:> midp FirstMIDlet Running the application in this way automatically launches the application and gives the output shown in Figure 3.2 above. This is fine if your application consists of one MIDlet. However, if your application consists of multiple MIDlets and you want to give the user a choice as to which one to run, you package the application. How to package the application is discussed in the next step. An example of having multiple MIDlets is shown in Enhancing the Application: Listing 5 below. Step 4: Packaging the application If an application consists of multiple classes, a JAR file is used to group all the classes together so that the application is easy to distribute and deploy. In my case, I created the JAR file as follows: c:> jar cf First.jar FirstMIDlet.class
The next step in packaging is creating a manifest file (or application descriptor),
which provides information about the contents of the JAR file. Manifest attributes
that start with
There is a predefined set of attributes to be used in every application descriptor.
For example, the descriptor file for the
The attributes are pretty much self-explanatory. However, the last attribute
should be discussed a little. The
The name appears in the device display so the MIDlet can be selected, the
icon (if one is supplied) appears next to the name, and the class is the
executable called to launch the MIDlet. The attribute name Step 5: Testing the packaged application
Once the application is packaged, we can test it to see if everything works.
To test the packaged application, use the c:> midp -descriptor first.jad If you are satisfied with the MIDlet, you can now deploy it to a web server by uploading the JAR and JAD files to the web server. Once deployed to a web server, your MIDlet is downloadable; however, you must add the following new MIME type to your mime-types configuration file and restart the web server before you can run the MIDlet: text/vnd.sun.j2me.app-descriptor jad Use the following command to download and launch the MIDlet: c:> midp -transient http://hostname/path/first.jad Enhancing Wireless Applications
In this section a ticker and icons are added to the
As you can see, the
Figure 4: Adding a Ticker
Now, we have two MIDlets:
I simply added both MIDlets to the application descriptor with these last two lines: MIDlet-1: First,, FirstMIDlet MIDlet-2: Hello,, HelloMIDlet
Figure 5: Two MIDlets Now when I run the application, I get what is shown in Figure 5. c:> midp -descriptor app.jad Users can now navigate through the items on the display (MIDlets) and select the one to run. Adding Icons We can make the application look more professional simply by adding icons to the items shown on the display. This is easily done by modifying the last two lines of the application descriptor in Listing 5 to read: MIDlet-1: First, /Tiles.png, FirstMIDlet MIDlet-2: Hello, /Stock.png, HelloMIDlet
Figure 6: Icons
I have added icons, in
Conclusion Programming MIDlets is fun. This article discusses the genesis and development life cycle of MIDlets. Example MIDlets with multiple commands and a running ticker were developed. The process of packaging, creating application descriptor files, and adding icons to real applications was presented through examples. Future articles will cover more advanced topics and MIDP programming examples showing how to make network connections, working with databases, and others. Stay tuned! Back To Top | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||