+ All Categories
Home > Documents > Lesson02-Getting Started

Lesson02-Getting Started

Date post: 08-Apr-2018
Category:
Upload: rowena-alipar
View: 232 times
Download: 0 times
Share this document with a friend

of 24

Transcript
  • 8/7/2019 Lesson02-Getting Started

    1/24

    J.E.D.I.

    Getting Started with

    Mobile Programming

    1 Objectives

    In this section, we will be delving into writing, building, using the emulator andpackaging J2ME applications. The Integrated Programming Environment that we will useis Netbeans 4.1 (www.netbeans.org) with Mobility Pack.

    After finishing this lesson, the student should be able to:

    Create a simple MIDlet

    Create a Project in Netbeans

    Create a MIDlet in Netbeans

    Run a MIDlet on the emulator

    2 Introduction

    An IDE (Integrated Development Environment) is a programming environment with aGUI builder, a text or code editor, a compiler and/or interpreter and a debugger. In ourcase, Netbeans also comes with a device emulator. This lets you see how your programwould look like in an actual device.

    3 "Hello, world!" MIDlet

    We have studied in the previous section the MIDlet's life cycle. A MIDlet's life starts whenit is created by the Application Management System (AMS) of the device. Initially, it is inthe "paused" state.

    To be able to create a MIDlet, we must create a subclass of the MIDlet class fromjavax.microedition.midlet package. We must also override or implement the methods:startApp(), destroyApp() and pauseApp(). These are the methods expected by the AMSfor running and controlling our MIDlet.

    Mobile Application Development 1

  • 8/7/2019 Lesson02-Getting Started

    2/24

    J.E.D.I.

    Unlike a typical Java program where the main() method is only entered once in theprogram's life, the startApp() method may be called more than once during the MIDlet'slife cycle. So you must not put one time initialization codes in the startApp() method.Instead, you can create a MIDlet constructor and do your initializations there.

    Here is the code of our first MIDP program:

    /*

    * HelloMidlet.java

    *

    * Created on July 8, 2000, 9:00 AM

    */

    import javax.microedition.midlet.*;

    import javax.microedition.lcdui.*;

    /**

    *

    * @author JEDI Apprentice

    * @version

    */

    public class HelloMidlet extends MIDlet implements CommandListener {

    Mobile Application Development 2

    Active

    Paused

    Destroyed

    destroyApp()

    destroyApp()

    startApp()

    pauseApp()

    new

  • 8/7/2019 Lesson02-Getting Started

    3/24

    J.E.D.I.

    Display display;

    Command exitCommand = new Command("Exit", Command.EXIT, 1);

    Alert helloAlert;

    public HelloMidlet(){

    helloAlert = new Alert(

    "Hello MIDlet", "Hello, world!",

    null, AlertType.INFO

    );

    helloAlert.setTimeout(Alert.FOREVER);

    helloAlert.addCommand(exitCommand);

    helloAlert.setCommandListener(this);

    }

    public void startApp() {

    if (display == null){

    display = Display.getDisplay(this);

    }

    display.setCurrent(helloAlert);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void commandAction(Command c, Displayable d){

    if (c == exitCommand){

    destroyApp(true);

    notifyDestroyed(); // Exit

    }

    }

    }

    Next, we would dissect our first MIDlet, focusing on every significant line of code:

    Mobile Application Development 3

  • 8/7/2019 Lesson02-Getting Started

    4/24

    J.E.D.I.

    public class HelloMidlet extends MIDlet implements CommandListener {

    As we have said before, we should create a subclass of MIDlet to create our MIDPprogram. In this line, we did created a subclass of MIDlet by extending it and naming itHelloMIdlet.

    Display display;

    Command exitCommand = new Command("Exit", Command.EXIT, 1);

    Alert helloAlert;

    These are the variable properties of our MIDlet. We would need the Display object (these

    is only one display associated per MIDlet) in order to do some drawing on the screen.The exitCommand is a command we would put in our screen so that we can quit theprogram. If we would not put any exit command, there would be no way to exit ourMIDlet gracefully.

    public HelloMidlet(){

    helloAlert = new Alert(

    "Hello MIDlet", "Hello, world!",

    null, AlertType.INFO

    );

    helloAlert.setTimeout(Alert.FOREVER);

    helloAlert.addCommand(exitCommand);

    helloAlert.setCommandListener(this);

    }

    The constructor initializes our Alert object. We would be discussing more of the Alertclass in the next sections. The addCommand() method on the Alert object puts an "Exit"command on the screen. The setCommandListener() method tells the system to pass allcommand events to our MIDlet.

    public class HelloMidlet extends MIDlet implements CommandListener {

    The "implements CommandListener" code is for command/key presses, so that ourprogram would be able to handle "command" events. If we implement aCommandListener, we must create a commandAction() method.

    public void commandAction(Command c, Displayable d){

    if (c == exitCommand){

    Mobile Application Development 4

  • 8/7/2019 Lesson02-Getting Started

    5/24

    J.E.D.I.

    destroyApp(true);

    notifyDestroyed(); // Exit

    }

    }

    Our commandAction() only handles request for the "Exit" command. It would terminateour program using notifyDestroyed() if the "Exit" command is issued or pressed.

    public void startApp() {if (display == null){

    display = Display.getDisplay(this);

    }

    display.setCurrent(helloAlert);

    }

    This is the entry point of our program once it is ready to be displayed by the AMS.Remember that the startApp() may be entered more than once in the MIDlet's life cycle.If a MIDlet is paused, say by an incoming phone call, it would enter the paused state(pauseApp). If after the call the AMS wants to bring back our program, it would call thestartApp() method again. The display.setCurrent() method tells the system that we wantour Alert object to be displayed on the screen. We can get the display object by callingthe static Display.getDisplay() method.

    Netbeans automatically creates a Java Application Descriptor (JAD) for your program.Netbeans put the JAD file in the "dist" folder under the project's folder. Here a sampleJAD file created by Netbeans:

    MIDlet-1: HelloMidlet, , HelloMidletMIDlet-Jar-Size: 1415MIDlet-Jar-URL: ProjectHello.jarMIDlet-Name: ProjectHelloMIDlet-Vendor: VendorMIDlet-Version: 1.0

    MicroEdition-Configuration: CLDC-1.1MicroEdition-Profile: MIDP-2.0

    Now we're ready to compile, package and run our first MIDlet.

    Mobile Application Development 5

  • 8/7/2019 Lesson02-Getting Started

    6/24

    J.E.D.I.

    4 Compilation and Packaging MIDlets

    Before we use any of the integrated tools for compiling and automatically packaging ourMIDlet suites, we'll try to do this using the command line tools.

    The MIDlet suite is usually packed into a single file called the JAR file. This is acompressed file, much like a ZIP file. In fact, you can open a JAR file using a ZIPdecompressor program.

    The MIDlet suite consists of:

    The JAR file

    The Java Application Descriptor (JAD) file

    The JAR file contains:

    The class files

    Manifest file describing the contents of the archive

    resources: images/icons, video, data, etc. used by the suite

    The Manifest file, manifest.mf is like the JAD file. It is used by the application manager ofthe device. The required fields of the manifest file are:

    MIDlet-Name

    MIDlet-Version

    MIDlet-Vendor

    MIDlet- (where n is a number from 1, for each MIDlet in the JAR file)

    MicroEdition-Profile

    MicroEdition-Configuration

    First we compile the java source files:

    javac -bootclasspath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar*.java

    Mobile Application Development 6

  • 8/7/2019 Lesson02-Getting Started

    7/24

    J.E.D.I.

    The Java Compiler program, "javac", should be on your path. If you see an error like,"cannot find file" or "not an executable", please consult the installation guide for your

    Java development kit distribution on how to put in your executable PATH the location ofthe java tools.

    Next, we pre-verify the class file(s):

    preverify

    -classpath C:\WTK23\lib\cldcapi11.jar;C:\WTK23\lib\midpapi20.jar;.

    -d . HelloMidlet

    preverify is included in the wireless toolkit from java.sun.com. Enter this command in asingle line.

    The final step is to create the JAR itself:

    jar cvfm HelloMidlet.jar manifest.txt HelloMidlet.class

    The jar program is included in the Java Development Kit, and its location should beincluded in your executable path. This command will create the JAR file with filenameHelloMidlet.jar. The manifest.txt would be renamed to manifest.mf in the JAR file.

    Mobile Application Development 7

  • 8/7/2019 Lesson02-Getting Started

    8/24

    J.E.D.I.

    5 Using the Sun Wireless Toolkit

    We'll now use the Sun Wireless Toolkit to compile and package our MIDlet suite(containing a single MIDlet).

    Open ktoolbar (from the Wireless Toolkit distribution):

    C reate a new project:

    Mobile Application Development 8

  • 8/7/2019 Lesson02-Getting Started

    9/24

    J.E.D.I.

    On this Settings window, you can customize the a very comprehensive selection ofsettings for your project. You can select what configuration to use, what packages/APIs

    to include, Push Registry settings and more. For our purposes, we will use the defaultproject settings. Click "OK" to finish creating the project.

    Mobile Application Development 9

  • 8/7/2019 Lesson02-Getting Started

    10/24

    J.E.D.I.

    Copy HelloMidlet.java into the project's "src directory: In Window, this is under thedirectory: C:\WTK23\apps\HelloMidlet\src (where C:\WTK23 is where you installed the

    wireless toolkit). Click "Build" and "Run":

    Mobile Application Development 10

  • 8/7/2019 Lesson02-Getting Started

    11/24

    J.E.D.I.

    Mobile Application Development 11

  • 8/7/2019 Lesson02-Getting Started

    12/24

    J.E.D.I.

    6 Using Netbeans and Mobility Pack

    As a pre-requisite for this section, Netbeans 4.1 and Mobility Pack should be installed onyour computer.

    Step 1: Start creating a new Project

    Mobile Application Development 12

  • 8/7/2019 Lesson02-Getting Started

    13/24

    J.E.D.I.

    Step 2: Select the "Mobile" Category

    Step 3: Select "Mobile Application"

    Mobile Application Development 13

  • 8/7/2019 Lesson02-Getting Started

    14/24

    J.E.D.I.

    Step 4: Name the Project and specify its location

    (Uncheck "Create Hello MIDlet, we would be creating our own MIDlet later)

    Step 5: Select the Platform (optional)

    Mobile Application Development 14

  • 8/7/2019 Lesson02-Getting Started

    15/24

    J.E.D.I.

    Mobile Application Development 15

    Figure 1: Newly Created Mobile Project (NetBeans)

  • 8/7/2019 Lesson02-Getting Started

    16/24

    J.E.D.I.

    Step 6: Start creating a new MIDlet

    Mobile Application Development 16

  • 8/7/2019 Lesson02-Getting Started

    17/24

    J.E.D.I.

    Step 7: Choose MIDP "Category" and MIDlet "File Type"

    Mobile Application Development 17

  • 8/7/2019 Lesson02-Getting Started

    18/24

    J.E.D.I.

    Step 8: Create a Name for the MIDlet

    Mobile Application Development 18

  • 8/7/2019 Lesson02-Getting Started

    19/24

    J.E.D.I.

    Mobile Application Development 19

    Figure 2: Creating a new MIDlet automatically creates required MIDlet methods

  • 8/7/2019 Lesson02-Getting Started

    20/24

    J.E.D.I.

    Step 10: Replace the automatically created code with our program's code.

    Mobile Application Development 20

  • 8/7/2019 Lesson02-Getting Started

    21/24

    J.E.D.I.

    Step 11: Compile and Run the MIDlet in the Emulator

    Mobile Application Development 21

  • 8/7/2019 Lesson02-Getting Started

    22/24

    J.E.D.I.

    Step 12: Launch our MIDlet on the Emulator

    Mobile Application Development 22

  • 8/7/2019 Lesson02-Getting Started

    23/24

    J.E.D.I.

    Mobile Application Development 23

    Figure 3: Hello World MIDlet in action

  • 8/7/2019 Lesson02-Getting Started

    24/24

    J.E.D.I.

    7 Exercises

    7.1 Multiple MIDlets in one MIDlet suite

    Add a new MIDlet to our project "ProjectHello". Take note that Netbeans automaticallyadds the new MIDlet in the the applications JAD file when you use the "New File..."Wizard.

    7.2 Multiple MIDlets in one MIDlet suite using the WirelessToolkit

    Use the Sun Wireless Toolkit to add a new MIDlet into your MIDlet suite.

    Mobile Application Development 24


Recommended