·
The MIDlet suite Environment
·
The getResourceAsStream method
·
The
JAM
When we develop a MIDlet, we
simply develop a class that extends the MIDlet class and let the JAM (Java
Application Manager) instantiating it and invoking different methods on it.The
different methods that the JAM invokes on the midlet change the midlet state.
As a result of that, the midlet goes through different states in its life
cycle. The midlet states during its life cycle are as follows:
When the JAM receives the
class (a class we declared as one that extends the MIDlet class) it
instantiates it and positions it in the Paused state.
When the JAM invokes the
startApp() method (on the midlet) the midlet moves to the Active
state.
While the midlet in its Active
state the JAM can invoke the pauseApp() method on it and by doing so, moves it
back to the Paused state. If the active midlet wants to return back to
the Paused state it can call the notifyPaused() method. By doing so, it
will be moved back to the Pause state.
While the midlet in its Active
state the JAM can invoke the destroyApp() method on it and by doing so, moves it
to the Destroyed state(in other words: destroy it). If the active midlet
wants be Destroyed it can do it self by calling the notifyDestroyed()
method. Calling the notifyDestroyed() method doesn’t invoke the destroyApp()
method. The midlet must have performed the same operations (clean up, releasing
of resources etc...) it would have if the MIDlet.destroyApp() had been called.
Once a midlet enters the Destroyed state, it cannot reenter any other state.
The midlet in its Paused
state can call the resumeRequest() method and signal (by doing so) to the JAM
that it wants to become active again. In response to that the JAM will call the
startApp() method on that midlet.
The startApp() method
might get called more than once. Therefore, any operations that must be
performed only once when the application is launched should be placed in the
constructor. The startApp() method may be called by the system under different
circumstances and its purpose is to prepare the midlet to handle dofferent
events as well as reaquire the needed resources. Eacn time, the midlet is
resumed the startApp() method is called.
The pauseApp() method job
is usually releasing as many resources as possible toward the next midlet
restart (when the startApp() method is invoked). Placing nulls in variables the
midlet has will cause indirectly the garbage collector to free these memory
spaces. When the midlet is in the Paused state it should hold as few resources
as possible. A midlet in the Paused state can receive asynchronous notifications
(from a timer firing, for instance).
The destroyApp() method
job is performing all the necessary clean up operations to release all the
resources the midlet had allocated during its execution (closing network
connections, database records etc…).
The destroyApp() method has one boolean parameter that indicates wheter
the midlet termination is unconditional (true) or not. If the boolean
parameter’s value is false then the midlet might throw
MIDletStateChangeException and by doing so, indicate the device that it wants
to stay alive.
The following diagram
demonstrates the different states in which the midlet can be.
The following midlet
demonstrates the different states in which the midlet can be. Even though the
System.out.println() commands are not seen when the midlet runs on a real
device, when the midlet is tested using one of the existing emulators the
System.out.println() commands are seen in the command line. Parts of the
following example deal with GUI and events handling. These topics will be explained
later.
//filename:HelloIsrael.java
//Copyright
(c) 2001 Haim Michael & Zindell Publishing House, Ltd.
//All
rights reserved. No part of the contents of this program may be
//reproduced
or transmitted in any form or by any means without the
//written
permission of the publisher.
import
javax.microedition.midlet.*;
import
javax.microedition.lcdui.*;
public
class MIDletStates extends MIDlet implements CommandListener
{
private
Display display;
private
Command exitCommand;
private
TextBox textBox;
public
MIDletStates()
{
System.out.println("The
MIDletStates constructor is invoked");
exitCommand
= new Command("Exit", Command.EXIT, 1);
textBox
= new TextBox("MIDlet's states demo", "Hello:)", 15,0);
textBox.addCommand(exitCommand);
textBox.setCommandListener(this);
}
public
void startApp()
{
System.out.println("The
startApp method is invoked");
display
= Display.getDisplay(this);
display.setCurrent(textBox);
}
public
void pauseApp()
{
System.out.println("The
pauseApp method is invoked");
}
public
void destroyApp(boolean cond)
{
System.out.println("The
destroyApp method is invoked");
}
public
void commandAction(Command command, Displayable displayable)
{
if(exitCommand==command)
{
destroyApp(true);
}
}
}
Manifest-Version:
1.0
MIDlet-1:
HelloIsrael, HelloIsrael.png, com.zindell.j2me.HelloIsrael
MIDlet-Name:
HelloIsrael
MIDlet-Version:
1.0
MIDlet-Vendor:
ZINDELL
MicroEdition-Configuration:
CLDC-1.0
MicroEdition-Profile:
MIDP-1.0
Manifest-Version:
1.0
MIDlet-1:
HelloIsraelProject, HelloIsraelProject.png, com.zindell.j2me.HelloIsrael
MIDlet-Jar-Size:
1056
MIDlet-Jar-URL:
HelloIsraelProject.jar
MIDlet-Name:
HelloIsraelProject
MIDlet-Vendor:
ZINDELL
MIDlet-Version:
1.0
//filename:HelloIsrael.java
//Copyright
(c) 2001 Haim Michael & Zindell Publishing House, Ltd.
//All
rights reserved. No part of the contents of this program may be
//reproduced
or transmitted in any form or by any means without the
//written
permission of the publisher.
import
javax.microedition.midlet.*;
import
javax.microedition.lcdui.*;
public
class HelloToWhom extends MIDlet
{
private
Display display;
private
Form form;
public
HelloToWhom()
{
String
str = getAppProperty("MIDPSuiteProperties.toWhom");
form
= new Form(str);
}
public
void startApp()
{
display
= Display.getDisplay(this);
display.setCurrent(form);
}
public
void pauseApp()
{
}
public
void destroyApp(boolean cond)
{
}
}
public InputStream
getResourceAsStream(String name)
public InputStream
getResourceAsStream(String name)
ZINDELL.COM
//filename:ResourceDemo.java
//Copyright (c) 2001 Haim Michael & Zindell
Publishing House, Ltd.
//All rights reserved. No part of the contents of this
program may be
//reproduced or transmitted in any form or by any
means without the
//written permission of the publisher.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class ResourceDemo extends MIDlet
{
private
Display display;
private
Command exitCommand, stopCommand;
private
TextBox textBox;
public
ResourceDemo()
{
try
{
Class
theClass = Class.forName("ResourceDemo");
InputStream
is = theClass.getResourceAsStream("/details.txt");
StringBuffer
sb = new StringBuffer();
int
temp = is.read();
while(temp!=-1)
{
sb.append((char)temp);
temp
= is.read();
}
String
str = sb.toString();
System.out.println("str="+str);
textBox
= new TextBox("Welcome To", str, 15,0);
}
catch(Exception
e)
{
}
}
public
void startApp()
{
display
= Display.getDisplay(this);
display.setCurrent(textBox);
}
public
void pauseApp()
{
}
public
void destroyApp(boolean cond)
{
}
}
Table of Contents
Chapter 1 : Let’s Start
Chapter 2 : The MIDlet
Chapter 3 : GUI
Chapter 4 : Persistent Storage
Chapter 5 : Networking
Chapter 6 : The Canvas class
Chapter 7 : Performance
Chapter 8 : XML
2000 © All the rights reserved to
Haim Michael & Zindell Publishing House Ltd.
No
parts of the contents of this paper may be reproduced or transmitted
in
any form by any means without the written permission of the publisher !
This
book can be used for personal use only !!!
Brought
to you by ZINDELL