Wednesday, 23 March 2011

J2ME


Java ME was designed by Sun Microsystems and is a replacement for a similar technology, Personal Java. Originally developed under the Java Community Process as JSR 68, the different flavors of Java ME have evolved in separate JSRs. Sun provides a reference implementation of the specification, but has tended not to provide free binary implementations of its Java ME runtime environment for mobile devices, rather relying on third parties to provide their own. As of 22 December 2006, the Java ME source code is licensed under the GNU General Public License, and is released under the project name phone.
Java ME has become a popular option for creating games for cell phones, as they can be emulated on a PC during the development stage and easily uploaded to the phone. This contrasts with the difficulty of developing, testing, and loading games for other special gaming platforms such as those made by Nintendo, Sony, Microsoft, and others, as expensive system-specific hardware and kits are required.
Usage of Java ME
Java ME includes flexible user interfaces, robust security, built-in network protocols, and support for networked and offline applications that can be downloaded dynamically. Applications based on Java ME are portable across many devices; yet leverage each device’s native capabilities.
Java ME device as implement a profile, the most common of these are the Mobile Information Device Profile aimed at mobile devices, such as cell phones, and the Personal Profile aimed at consumer products and embedded devices like Set-top boxes and PDAs.
Developing with Java ME
Writing a Java ME application uses the same basics programming constructs as used with Java SE applications. Basically there are two types of configurations involved in Java ME application development, which are:
CLDC (Connected Limited Device Configuration)
CDC (Connected Device Configuration)
Architecture The Java ME Architecture comprises of three software layers:
The first layer is the configuration layer that includes the JVM, which directly interacts with the native OS. The Configuration layer also handles the interaction between the profile and the JVM.

The second layer is the profile layer, which consists of the minimum set of application programming interface (API) for the small devices.
The third layer is the Mobile Information Device profile (MIDP) layer. The MIDP layer contains java APIs for user network connections, persistence storage, and the user interface. It also has access to CLDC libraries and MIDP libraries.
The Java ME Application Development
I. System Requirements - Hardware
Minimum hardware requirements are:
  • 100 MB hard disk space
  • 128 MB system RAM
  • 800 MHz Pentium III CPU
II. Minimal Software Requirement
  • IDE – Sun ONE Studio 4, Mobile Edition,
    (formerly Forte for Java)
  • GUI – Sun Java ME Wireless Toolkit 2.5.1 (WTK 2.5.1) for CLDC
For Windows: Download the Sun Java Wireless Toolkit for CLDC from http://java.sun.com/products/ s j w t o o l k i t / d o w n l o a d. h t m l Ensure that you have installed an appropriate
Java SE environment. Run the installer, sun_java_wireless_toolkit- 2_5_1-windows.exe. Follow the instructions provided by the installer.

Download the Sun Java Wireless Toolkit for CLDC from http://java.sun.com/products/ s j w t o o l k i t / d o w n l o a d . h t m l Ensure that you have installed an appropriate
Java SE environment
Run the installer, sun_java_wireless_toolkit- 2_5_1-linux.exe. Follow the instructions provided by the installer.

III. install Sun Java ME Wireless Toolkit 2.5.1 (WTK 2.5.1) on the Windows platform.




  • Download the installer file i.e. netbeans_mobility-5_5_1-win.exe
  • Double Click the icon of downloaded exe.
Now we are ready to create an application with Java Platform ME. Lets create a new project with the following steps:
Step 1: Go to Windows start panel and choose" Wireless Toolkit 2.5.1” as: Start > Programs > Sun Java Wireless Toolkit 2.5.1 for CLDC > Wireless Toolkit 2.5.1. The console window appears like this.
Step 2: Now, Click the “New Project” on the toolkit menu bar, then a new project box opens. Fill the appropriate Project name and MIDlet class Name of your choice. After that, click Create Project button.
Step 3: Now, Click the “New Project” on the toolkit menu bar, then a new project box opens. Fill the appropriate Project name (opensourzesupport)and MIDlet class Name(OpensourzesupportMID) of your choice. After that, click Create Project button.


Step 4:
 Then a “Settings for project” window appears. For default settings, click OK. It is

Step 5: Next appears a window indicating the updated project settings saved in the Console.


import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class HelloWorld extends MIDlet implements CommandListener {
public void startApp() {
Display display = Display.getDisplay(this);

Form mainForm = new Form("HelloWorld");
mainForm.append("Welcome to the world of Mobile");

Command exitCommand = new Command("Exit", Command.EXIT, 0);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);

display.setCurrent(mainForm);
}
public void pauseApp () {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
}
}
Here is the code of this program:

Step 6: Now we need to develop a simplest "Hello World" program in the directory structure in src folder.


Step 7: 
Now, Click the “Open Project” on the toolkit menu bar, 
Step 8: Next click the “Build” button from the toolkit menu bar. This causes the Sun Java Wireless Toolkit for CLDC to compile and preverify the Java source files. The whole build process is shown below.

Step 9: Next click the “Run” button from the toolkit menu bar. This executes the compiled Java class files on the emulator.


Midlet Lifecycle


A MIDlet lifecycle have following steps...
  • startApp()
  • pauseApp()
  • destroyApp()
By default MIDlet is in the paused states. when the application is executed by default startApp() method will call and when close the application thedestroyApp() method will be called. But when your constructor is not null type then it will be executed firstly. The source code of life cycle execution is as follows:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class MidletLifecycle extends MIDlet{
  private Form form;
  
  private Display display;

  public MidletLifecycle(){
    System.out.println("MidletLifecycle constructure");
  }

  public void startApp(){
    form = new Form("Midlet Lifecycle");
    display = Display.getDisplay(this);
    String msg = "This is the Lifecycle of Midlet!";
    form.append(msg);
    display.setCurrent(form);
  }

  public void pauseApp(){
    System.out.println("You are in pauseApp()...");
  }

  public void destroyApp(boolean destroy){
    System.out.println("You are in destroyApp()...");
    notifyDestroyed();
  }
}





Fig: MIDlet Lifecycle
Output:

Source code of 'jad' and 'properties' file


Java Application Descriptor (JAD) filename extension is .jad and media type is text/vnd.sun.j2me.app-descriptor, which developed by the Sun Microsystems, Inc. The JAD file is used for java or games application. The java application enabled mobile phone connected programmatically with online Web Services. Through this facility we can send SMS via GSM mobile Internet.          
  
Tutorials.jad
MIDlet-1: LoginExample, , LoginExample
MIDlet-Jar-Size: 4165
MIDlet-Jar-URL: LoginPage.jar
MIDlet-Name: LoginPage
MIDlet-Vendor: Unknown
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0

The jad file formate is as follows:        
MIDlet-1: <Application name>, <icon path>, <midlet class>
MIDlet-Jar-Size:<Size in bytes>
MIDlet-Jar-URL:<Associated JAR file>
MIDlet-Name:<Application name>
MIDlet-Vendor:<Company>
MIDlet-Version:<Application version number>
MicroEdition-Configuration:<CLDC version>
MicroEdition-Profile:<MIDP version>

Source code of project.properties is as follows:
JSR082: false
JSR172: true
JSR184: false
JSR75: false
MMAPI: true
WMA2.0: false
configuration: CLDC1.0
platform: JTWI

Source code of MANIFEST.MF is as follows:
MIDlet-1: LoginExample, , LoginExample
MIDlet-Name: LoginPage
MIDlet-Vendor: Unknown
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0

J2ME Hello World Example



This is the simple hello world application. In this example we are creating a form name "Hello World" and creating a string message "Hello World!!!!!!!" as below: 
Form form = new Form("Hello World");     
String msg = "Hello World!!!!!!!";
In this application, To display the message we are using append method with the form as below:
 form.append(msg);
  
The Application is as follows:

HelloWorld.java

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

public class HelloWorld extends MIDlet{
  private Form form;
  private Display display;

  public HelloWorld(){
    super();
  }

  public void startApp(){
    form = new Form("Hello World");
    String msg = "Hello World!!!!!!!";
    form.append(msg);
    display = Display.getDisplay(this);
    display.setCurrent(form);
  }

  public void pauseApp(){}
  
  public void destroyApp(boolean unconditional){
    notifyDestroyed();
  }
}



MIDlet Application For Login in J2ME


This example show to create the MIDlet application for user login . All MIDlet applications for the MIDP ( Mobile Information Device Profile) derived from  MIDlet class and it play a role as a mediator between the application and the environment in which the application runs. The MIDlet life cycle manage the flow of application. It is in thejavax.microedition.midlet package, so import this package in your application. Thejavax.microedition.icdui package is used for following classes:
  • Alert
  • AlertType
  • Canvas
  • ChoiceGroup
  • Command
  • DateField
  • Display
  • Displayable
  • Font
  • Form
  • Gauge
  • Graphics
  • Image
  • ImageItem
  • Item
  • List
  • Screen
  • StringItem
  • TextBox
  • TextField
  • Ticker 




In this example we will create a MIDlet (LoginExample), that will show following output display look like below: 

        
Click on 'Launch' Button then the login page display like below:

Give Your LoginID 'prasobh' and Password 'prasobh' then it call the commandAction where if condition will be executed and it calls a function (validateUser()) which checks whether name and password is equal to 'prasobh' or not if it equal to 'prasobh' then it executed the showMsg()function which show a congratulation message but if name and password is not equal to 'prasobh' then it call tryAgain() function which show the error page like figure below and it return on login page with refresh value.




Source Code Of LoginExample.java
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class LoginExample extends MIDlet implements CommandListener{
  private Display display;
  private TextField userName,password;
  public Form form;
  private Command login,cancel;
  private Image img, imge, img2;
      
  public LoginExample() {
    form = new Form("Sign in");
    userName = new TextField("LoginID:"""30, TextField.ANY);
    password = new TextField("Password:"""30, TextField.PASSWORD);
    cancel = new Command("Cancel", Command.CANCEL, 2);
    login = new Command("Login", Command.OK, 2);
    try{
      img = Image.createImage("/logo.png");
      imge = Image.createImage("/front_left1_bad.png");
      img2 = Image.createImage("/Congratulations-1.png");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }    
  }

   public void startApp() {
    display = Display.getDisplay(this);
    try{form.append(img);}catch(Exception e){}
    form.append(userName);
    form.append(password);
    form.addCommand(cancel);
    form.addCommand(login);
    form.setCommandListener(this);
    display.setCurrent(form);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {
    notifyDestroyed();
  }

  public void validateUser(String name, String password) {
    if (name.equals("prasobh"&& password.equals("prasobh")) {
      showMsg();
    else {
      tryAgain();
    }
  }  

  public void showMsg() {
    Alert success = new Alert("Login Successfully"
    "Your Login Process is completed!"
,
     img2, AlertType.INFO
);
    success.setImage(img2);
    userName.setString("");
    password.setString("");
    display.setCurrent(success, form);    
  }

  public void tryAgain() {
    Alert error = new Alert("Login Incorrect""Please
    try again"
, imge, AlertType.ERROR);
    error.setTimeout(900);
    error.setImage(imge);
    userName.setString("");
    password.setString("");
    display.setCurrent(error, form);
  }
  
  public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if(label.equals("Cancel")) {
      destroyApp(true);
    else if(label.equals("Login")) {
      validateUser(userName.getString(), password.getString());
    
  }
}

No comments:

Post a Comment