What is J2ME with simple example ?


The Java 2 Micro Edition (J2ME): J2ME is a Java platform that is designed for small devices. It contains specially designed, lightweight virtual machines; a bare minimum of core-class libraries; and lightweight substitutes for standard Java libraries. J2ME is the ideal mobile client platform for wireless PDAs and enhanced mobile phones.
J2ME is divided into configurations, profiles, and optional APIs, which provide specific information about APIs and different families of devices.
 A configuration is designed for a specific kind of device based on memory constraints and processor power. It specifies a Java Virtual Machine (JVM) that can be easily ported to devices supporting the configuration. It also specifies some subset of the Java 2 Platform, Standard Edition (J2SE) APIs that will be used on the platform, as well as additional APIs that may be necessary.
Profiles are more specific than configurations. A profile is based on a configuration and adds APIs for user interface, persistent storage, and whatever else is necessary to develop running applications.
Optional APIs define specific additional functionality that may be included in a particular configuration.

Configuration:
The two most important J2ME configurations are as follows.
             The Connected Limited Device Configuration (CLDC) is for the smallest wireless devices with 160 KB or more memory and slow 16/32-bit processors.
             The connected Device Configuration (CDC) is for more capable wireless devices with at least 2 MB of memory and 32-bit processors. Unlike the CLDC, the CDC supports a fully featured Java 2 VM and therefore can take advantage of most J2SE libraries
Profile:
The most important and successful J2ME profile is the Mobile Information Device Profile (MIDP), based on the CLDC. The MIDP targets the smallest devices, such as cell phones. It is already deployed on millions of handsets
The APIs available to a MIDP application come from packages in both CLDC and MIDP
CLDC
 java.lang
java.lang.ref
java.io
java.util
javax.microedition.io

MIDP
javax.microedition.lcdui
javax.microedition.lcdui.game
javax.microedition.media
javax.microedition.media.control
javax.microedition.midlet
javax.microedition.pki
javax.microedition.rms


CLDC defines a core of APIs, mostly taken from the J2SE world. These include fundamental language classes in java.lang, stream classes from java.io, and simple collections from java.util. CLDC also specifies a generalized network API in javax.microedition.io.
“A Java program written to be executed on a MID is called a MIDlet”.
MIDP applications are represented by instances of the javax.microedition. midlet.MIDlet class. MIDlets have a specific life cycle, which is reflected in the methods and behavior of the MIDlet class.
The MIDlet Life Cycle
A MIDlet goes through the following states:
1.            When the MIDlet is about to be run, an instance is created. The MIDlet's constructor is run, and the MIDlet is in the Paused state.
2.            Next, the MIDlet enters the Active state after the application manager calls startApp().
3.            While the MIDlet is Active, the application manager can suspend its execution by calling pauseApp(). This puts the MIDlet back in the Paused state. A MIDlet can place itself in the Paused state by calling notifyPaused().
4.            The application manager can terminate the execution of the MIDlet by calling destroyApp(), at which point the MIDlet is destroyed and patiently awaits garbage collection. A MIDlet can destroy itself by calling notifyDestroyed().
MIDlet 
javax.microedition.midlet. MIDlet  is an abstract class which provides the basic functional overhead required for a MIDP application that can execute on a mobile device.
MIDlet methods:
startApp - the application is moving from the paused state to the active state. Initialization of objects needed while the application is active should be done.
protected void startApp() throws MIDletStateChangeException
pauseApp - the application may pause its threads. Also, if it is desirable to start with another screen when the application is re-activated
protected void pauseApp()
destroyApp - the application should free resources, terminate threads, etc. The behavior of method calls on user interface objects after destroyApp() has returned is undefined.
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
Display
javax.microedition.lcdui.Display represents the manager of the display and input devices of the system. It includes methods for retrieving properties of the device and for requesting that objects be displayed on the device.
There is exactly one instance of Display per MIDlet and the application can get a reference to that instance by calling the getDisplay() method. The application may call the getDisplay() method at any time during course of its execution. The Display object returned by all calls to getDisplay() will remain the same during this time.
Display methods:
getDisplay(): A static method to get instance of Display for a MIDlet.
public static Display getDisplay(MIDlet m)
setCurrent(): for setting the current Displayable
public void setCurrent(Displayable nextDisplayable)
getCurrent() method for retrieving the current Displayable
public Displayable getCurrent()
Displayable
javax.microedition.lcdui.Displayable
An object that has the capability of being placed on the display. A Displayable object may have a title, a ticker, zero or more commands and a listener associated with it. A Displayable objects are
Alert, Form, List, TextBox, LoginScreen, SplashScreen, WaitScreen, FileBrowser, PIMBrowser, SMSComposer.
Item
javax.microedition.lcdui.Item
A superclass for components that can be added to a Form. All Item objects have a label field, which is a string that is attached to the item. The label is typically displayed near the component when it is displayed within a screen. Item objects are
ChoiceGroup, DateField, Gauge, ImageItem, Spacer, StringItem, TextField , TableItem
Command
javax.microedition.lcdui
The Command class is a construct that encapsulates the semantic information of an action. The behavior that the command activates is not encapsulated in this object. This means that command contains only information about "command" not the actual action that happens when command is activated. The action is defined in a CommandListener associated with the Displayable.
The application uses the command type to specify the intent of this command. For example, if the application specifies that the command is of type BACK, and if the device has a standard of placing the "back" operation on a certain soft-button, the implementation can follow the style of the device by using the semantic information as a guide. The defined types are BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, and STOP.
Example: Get name from TextField and display on the screen
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class HelloApp extends MIDlet implements CommandListener {
  private Display display;

  private Form myForm = new Form("Enter Your Name");

/* Command(String label, int commandType, int priority), The application uses the priority value to describe the importance of this command relative to other commands on the same screen.
A SCREEN command generally applies to the entire screen's contents or to navigation among screens.  */

  private Command submit = new Command("Submit", Command.SCREEN, 1);

//A EXIT command used for exiting from the application.
  private Command exit = new Command("Exit", Command.EXIT, 1);

  private TextField txt1 = new TextField("Enter Name:", "", 30, TextField.ANY);

  public HelloApp() {
     //get a display instance.
    display = Display.getDisplay(this);
    myForm.addCommand(exit);
    myForm.addCommand(submit);
    myForm.append(txt1);
    myForm.setCommandListener(this);
  }
  public void startApp() {
//in display set myForm visible
    display.setCurrent(myForm);
  }
  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command == submit) {
      txt1.setString("Hello, " + txt1.getString());
      myForm.removeCommand(submit);
    } else if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}


No comments:

Post a Comment