The applications for mobile phones are written in form of MIDlets. Like applets are the java applications for the web pages, the MIDlets are the java applications for the mobile phones. The Java 2 Platform, Micro Edition (J2ME) includes the Mobile Information Device Profile (MIDP) which includes APIs specifically tailored for the mobile phones. The MIDP contains the ‘javax.microedition.midlet’ package which contains the MIDlet class. Every application written using MIDP should have at least one class which extends the MIDlet class. This is the class to which the control is transferred by the application management software of the mobile phone. This class contains methods which let the application management software to do the following tasks: § Create a MIDlet The constructor of the class which extends the MIDlet class is called by the application management software to create its instance. BEST PRACTICE You should remember that the constructor should return quickly. Also avoid initializing the resources in this method. The startApp () is the suitable method for the initialization of resources. § Start a MIDlet When a MIDlet enters
the active stage, the startApp () method is called. By experience I will
suggest you do all the heavy initializations of forms, lists and canvases in
the startApp () method. It is the startApp () method which signals that a
MIDlet has entered the active stage. Since the holding of resources is allowed
in the active stage, all the resources which are required initially at the
start of the application should be created from this method. Although from a
usability perspective, you should keep in mind that initially an application
should be loaded in a fast way. Thus all the unimportant initializations should
be done later in order for this method to return quickly. BEST PRACTICE You should also keep in mind that the startApp () method may be called many times by the application management software. Every time a MIDlet enters the active state from the paused state the startApp () method is called. Thus you should ordinarily declare a boolean method which is set to true when the startApp () method runs for the first time. Place all the initialization code within an ‘if’ statement which checks the state of the boolean variable. If its state is false i.e. when the startApp () is called the first time the initialization code should run else it should not. Do remember to flip this boolean variable when the initialization code runs so that the next time this code would not run. § Pause a MIDlet Whenever the application is interrupted due to phone calls, SMS,MMS or any other interruption the application management software calls the pauseApp () method. This method should normally implement the functionality for pausing the working of an application. BEST PRACTICE In games which contain timed movements or in applications which update the data automatically after a predetermined time, this functionality is very important in order for the state of the application to remain the same when the application resumes after the interruption is over. Also this saves the wasting of precious computing resources on the mobile phone. Thus you should make a point to have the pause functionality in the above specified scenarios. § Destroy a MIDlet The destroyApp () method signals to the MIDlet that it should enter the destroyed state. The MIDlet should release all the resources before destroying itself. BEST PRACTICE Although as per the official Sun documentation the application should save its persistent state here practically I will advise you against this. It has been my experience that it is better to save any persistent data in the RMS in the active state of the MIDlet. This method should return quickly and thus operations for persistent storage would not work well here.
Example Code 1: MIDlet /* * MyMIDlet.java */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /* * @author SKJ * @version 1.0.0 */ public class MyMIDlet extends javax.microedition.midlet.MIDlet { private Display display; private Form form ; private boolean runOnce ; public void startApp() { if(runOnce == false) { // Initialization code this.display = Display.getDisplay(this) ; this.form = new Form("MIDlet Example") ;
// Flipping the state of the boolean variable as discussed above runOnce = true ; } //Setting current display this.display.setCurrent(form) ; } public void pauseApp() { // No code in it due to the simplicity of the example } public void destroyApp(boolean unconditional) { // No code in it due to the simplicity of the example } } Emulator used for taking screen shots : Motorola_i85s CHECKLIST FOR MIDLET CREATION
|
Mobile Technology > Java ME (J2ME) > Book - Mobile Phone Programming using Java ME (J2ME) > UNIT II >