Saturday 18 April 2015

HB Blog 68: Midlet Programming.

A MIDlet is an application that uses the Mobile Information Device Profile (MIDP) of the Connected Limited Device Configuration (CLDC) for the Java ME environment. Sun Microsystems uses the "let" suffix to name several different types of programs that fall within the Java technology umbrella. Applets, servlets,etc.

The development process for MIDlet is as follows,
1)Edit 
Sun Java Wireless Toolkit (SJWTK) is an environment used as editore for midlet programming. It is similar to java Applet.
The editor provides all features that are essential to develop midlet program.
2)Compile
Compilation is the process of compiling the Java source files into executable byte code. The compiler of the virtual machine compiles the Java source code.
3)Verification
Before execution process the pre-verification is process is performed. The byte code is verified as security aspect and then signed .jar file is created.
4)Run
Finally, the midelet program is ready to run on MIDP devices.

A MIDlet's main class extends javax.microedition.midlet.MIDlet. The main class defines three life-cycle notification methods: startApp(), pauseApp(), and destroyApp().
There are three possible states in a MIDlet's life-cycle:
Paused: The MIDlet instance has been constructed and is inactive. 
Active: The MIDlet is active. 
Destroyed: The MIDlet has been terminated and is ready for reclamation by the garbage collector.

The startApp() lifecycle method initiates active state whereas, pausedApp() method handles paused state. And destroyApp() method is used to manage destroyed state.

Here is a hello world program for MIDlet application,

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet implements CommandListener {
    private Command exitCommand;
    private TextBox tbox;

    public HelloWorld() {
        exitCommand = new Command("Exit", Command.EXIT, 1);
        tbox = new TextBox("Hello world MIDlet", "Hello World!", 25, 0);
        tbox.addCommand(exitCommand);
        tbox.setCommandListener(this);
    }

    protected void startApp() {
        Display.getDisplay(this).setCurrent(tbox);
    }

    protected void pauseApp() {}
    protected void destroyApp(boolean bool) {}

    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}

No comments:

Post a Comment