Programming for J2ME

Romulus Grigoras

ENSEEIHT Life-cycle of Applications

● Unmanaged (supported by CDC and CDLC, but not by MIDP) public class NeverEnding extends Thread { public void run(){ while( true ){ try { sleep( 1000 ); } catch( InterruptedException e ){ } ● Managed } } – Applets public static void main( String[] args ){ NeverEnding ne = new NeverEnding(); – ne.start(); } – }

Bibliography: http://developers.sun.com/techtopics/mobility/midp/articles/models/ The Applet Model

● java.applet.Applet (extends java.awt.Panel) ● supported by the Personal Profile ● states

– loaded public class BasicApplet extends java.applet.Applet { public BasicApplet(){ – stopped // constructor - don't do much here } – started public void init(){ // applet context is ready, perform all one-time – destroyed // initialization here } public void start(){ // the applet is being displayed } public void stop(){ // the applet is being hidden } public void destroy(){ } } The Midlet Model

MIDP 1.0 Specification:

● The application management software provides an environment in which the MIDlet is installed, started, stopped, and uninstalled. It is responsible for handling errors during the installation, execution, and removal of MIDlet suites and interacting with the user as needed. ● It provides to the MIDlet(s) the Java runtime environment required by the MIDP Specification.

● javax.microedition..MIDlet MIDP Event Handling

● Bibliography: http://developers.sun.com/techtopics/mobility/midp/articles/event/ The Midlet Model (2)

● managed by special-purpose application-mgmt software (AMS) built into the device ● may be interrupted at any point by outside events ● import javax.microedition.midlet.*; public class BasicMIDlet extends MIDlet { public BasicMIDlet(){ ● // constructor - don't do much here states } – protected void destroyApp( boolean unconditional ) paused throws MIDletStateChangeException { – // called when the system destroys the MIDlet active } – protected void pauseApp(){ destroyed // called when the system pauses the MIDlet } protected void startApp() throws MIDletStateChangeException { // called when the system activates the MIDlet } }

● javax.microedition.midlet.MIDlet The Midlet Context (1)

● Methods that let a MIDlet interact with its operating context: – getAppProperty() returns initialization property values – resumeRequest() asks the AMS to reactivate the MIDlet – notifyPaused() shifts the MIDlet to the paused state – notifyDestroyed() shifts the MIDlet to the destroyed state. The Midlet Context (2)

● MIDlet initialization properties are name-value pairs in the MIDlet's application descriptor or in its manifest ● the application descriptor = text file that lists important information about a set of MIDlets packaged together into a single JAR file (a MIDlet suite) ● the manifest is the standard JAR manifest packaged with the MIDlet suite ● when getAppProperty() is invoked, it searches the application descriptor first, then the manifest ● MIDP 2.0 introduces the concept of trusted suites, in which case getAppProperty() searches only the manifest A Better Midlet import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class BetterMIDlet extends MIDlet { private Display display; public BetterMIDlet(){ } protected void destroyApp( boolean unconditional ) throws MIDletStateChangeException { exitApp(); // call cleanup code } protected void pauseApp(){ // add pause code here } protected void startApp() throws MIDletStateChangeException { if( display == null ){ initApp(); // perform one-time initialization } // add per-activation code here } private void initApp(){ display = Display.getDisplay( this ); // add initialization code here } public void exitApp(){ // add cleanup code here notifyDestroyed(); // destroys MIDlet } } The Model

● Originally part of the Java TV API, Xlets have been adopted for use in the Personal Basis Profile (PBP) and in its superset, the Personal Profile (PP) import javax.microedition.xlet.*; public class BasicXlet implements Xlet { private XletContext context; public BasicXlet(){ // constructor - don't do much here } public void initXlet( XletContext context ) throws XletStateChangeException { // called by the system to initialize the Xlet this.context = context; // stash the context // put other initialization code here } public void destroyXlet( boolean unconditional ) throws XletStateChangeException { // called when the system destroys the Xlet } public void pauseXlet(){ // called when the system pauses the Xlet } public void startXlet() throws XletStateChangeException { // called when the system activates the Xlet } } Installation of a Midlet

● Direct install from files (stored on a memory card or received by Bluetooth/infrared) ● OTA (Over The Air) – download by http from a web server – the jad file has a pointer to the jar file on the server – MIDlet-Jar-URL : http://server/dir/file.jar Launching a MIDlet : Push

"Push" : mechanism to receive and act on information asynchronously, as information becomes available, instead of forcing the application to use synchronous polling techniques that increase resource use or latency ● network- and timer-initiated MIDlet activation;

Biblio: http://developers.sun.com/techtopics/mobility/midp/articles/pushreg/ Push MIDlet activation and lifecycle Programming for J2ME

Romulus Grigoras

ENSEEIHT Life-cycle of Java Applications

● Unmanaged (supported by CDC and CDLC, but not by MIDP) public class NeverEnding extends Thread { public void run(){ while( true ){ try { sleep( 1000 ); } catch( InterruptedException e ){ } ● Managed } } – Applets public static void main( String[] args ){ NeverEnding ne = new NeverEnding(); – Midlets ne.start(); } – Xlets }

Bibliography: http://developers.sun.com/techtopics/mobility/midp/articles/models/

Although the operating system can terminate the application without notice at any time, the application effectively has complete control over its life-cycle - when it terminates and when it acquires or releases system resources. The application is really an unmanaged application; the operating system participates only in its activation. If the operating system suddenly terminates the application, the application may not have the chance to save its state or release the system resources it has acquired. The Applet Model

● java.applet.Applet (extends java.awt.Panel) ● supported by the Personal Profile ● states

– loaded public class BasicApplet extends java.applet.Applet { public BasicApplet(){ – stopped // constructor - don't do much here } – started public void init(){ // applet context is ready, perform all one-time – destroyed // initialization here } public void start(){ // the applet is being displayed } public void stop(){ // the applet is being hidden } public void destroy(){ } } The Midlet Model

MIDP 1.0 Specification:

● The application management software provides an environment in which the MIDlet is installed, started, stopped, and uninstalled. It is responsible for handling errors during the installation, execution, and removal of MIDlet suites and interacting with the user as needed. ● It provides to the MIDlet(s) the Java runtime environment required by the MIDP Specification.

● javax.microedition.midlet.MIDlet MIDP Event Handling

● Bibliography: http://developers.sun.com/techtopics/mobility/midp/articles/event/ The Midlet Model (2)

● managed by special-purpose application-mgmt software (AMS) built into the device ● may be interrupted at any point by outside events ● import javax.microedition.midlet.*; public class BasicMIDlet extends MIDlet { public BasicMIDlet(){ ● // constructor - don't do much here states } – protected void destroyApp( boolean unconditional ) paused throws MIDletStateChangeException { – // called when the system destroys the MIDlet active } – protected void pauseApp(){ destroyed // called when the system pauses the MIDlet } protected void startApp() throws MIDletStateChangeException { // called when the system activates the MIDlet } }

● javax.microedition.midlet.MIDlet The Midlet Context (1)

● Methods that let a MIDlet interact with its operating context: – getAppProperty() returns initialization property values – resumeRequest() asks the AMS to reactivate the MIDlet – notifyPaused() shifts the MIDlet to the paused state – notifyDestroyed() shifts the MIDlet to the destroyed state. The Midlet Context (2)

● MIDlet initialization properties are name-value pairs in the MIDlet's application descriptor or in its manifest ● the application descriptor = text file that lists important information about a set of MIDlets packaged together into a single JAR file (a MIDlet suite) ● the manifest is the standard JAR manifest packaged with the MIDlet suite ● when getAppProperty() is invoked, it searches the application descriptor first, then the manifest ● MIDP 2.0 introduces the concept of trusted suites, in which case getAppProperty() searches only the manifest A Better Midlet

import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class BetterMIDlet extends MIDlet { ● Click t o aprdivdat ane Di sopluayt lidniseplay; public BetterMIDlet(){ } protected void destroyApp( boolean unconditional ) throws MIDletStateChangeException { exitApp(); // call cleanup code } protected void pauseApp(){ // add pause code here } protected void startApp() throws MIDletStateChangeException { if( display == null ){ initApp(); // perform one-time initialization } // add per-activation code here } private void initApp(){ display = Display.getDisplay( this ); // add initialization code here } public void exitApp(){ // add cleanup code here notifyDestroyed(); // destroys MIDlet } } The Xlet Model

● Originally part of the Java TV API, Xlets have been adopted for use in the Personal Basis Profile (PBP) and in its superset, the Personal Profile (PP) import javax.microedition.xlet.*; public class BasicXlet implements Xlet { private XletContext context; public BasicXlet(){ // constructor - don't do much here } public void initXlet( XletContext context ) throws XletStateChangeException { // called by the system to initialize the Xlet this.context = context; // stash the context // put other initialization code here } public void destroyXlet( boolean unconditional ) throws XletStateChangeException { // called when the system destroys the Xlet } public void pauseXlet(){ // called when the system pauses the Xlet } public void startXlet() throws XletStateChangeException { // called when the system activates the Xlet } } Installation of a Midlet

● Direct install from files (stored on a memory card or received by Bluetooth/infrared) ● OTA (Over The Air) – download by http from a web server – the jad file has a pointer to the jar file on the server – MIDlet-Jar-URL : http://server/dir/file.jar Launching a MIDlet : Push

"Push" : mechanism to receive and act on information asynchronously, as information becomes available, instead of forcing the application to use synchronous polling techniques that increase resource use or latency ● network- and timer-initiated MIDlet activation;

Biblio: http://developers.sun.com/techtopics/mobility/midp/articles/pushreg/ Push MIDlet activation and lifecycle

● Click to add an outline