Java Reference
In-Depth Information
application running in the foreground and unnecessary battery consumption. The
destroyApp()
method provides an unconditional parameter; if it is set to false, the MIDlet is allowed to refuse its
termination by throwing a
MIDletStateChangeException
. MIDlets can request to resume
activity by calling
resumeRequest()
. If a MIDlet decides to go to the paused state, it should
notify the application manager by calling
notifyPaused()
. In order to terminate, a MIDlet can
call
notifyDestroyed()
. Note that
System.exit()
is not supported in MIDP and will
throw an exception instead of terminating the application.
Note
Some devices might terminate a MIDlet under some circumstances without calling
destroyApp
(), for example on incoming phone calls or when the batteries are exhausted. Thus,
it might be dangerous to rely on
destroyApp()
for saving data entered or modified by the user.
Display and Displayable
MIDlets can be pure background applications or applications interacting with the user. Interactive
applications can get access to the display by obtaining an instance of the
Display
class. A
MIDlet can get its
Display
instance by calling
Display.getDisplay(MIDlet midlet)
,
where the MIDlet itself is given as parameter.
The
Display
class and all other user interface classes of MIDP are located in the package
javax.microedition.lcdui
. The
Display
class provides a
setCurrent()
method that
sets the current display content of the MIDlet. The actual device screen is not required to reflect
the MIDlet display immediately—the
setCurrent()
method just influences the internal state of
the MIDlet display and notifies the application manager that the MIDlet would like to have the
given
Displayable
object displayed. The difference between
Display
and
Displayable
is
that the
Display
class represents the display hardware, whereas
Displayable
is something
that can be shown on the display. The MIDlet can call the
isShown()
method of
Displayable
in order to determine whether the content is really shown on the screen.
HelloMidp Revisited
complete MIDlet. Now that you have the necessary foundation, you can revisit
HelloMidp
from
an API point of view.
First, you import the necessary
midlet
and
lcdui
packages:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
Like all MIDP applications, the
HelloMidp
example is required to extend the
MIDlet
class:
public class HelloMidp extends MIDlet {
In the constructor, you obtain the
Display
and create a
Form
:
Display display;
Form mainForm;
public HelloMidp() {
Search WWH ::
Custom Search