Java Reference
In-Depth Information
the same suite run in the same space. A new Java ME run-time process
is spawned for every suite and all MIDlets from that suite run within that
process and in the same Java memory space; this implies that MIDlets
from the same suite can communicate, programmatically.
For example, MIDlet1 and MIDlet2 are both in the same suite:
public class MIDlet2 extends MIDlet implements CommandListener {
public static int value = 0;
public static MIDlet2 instance;
public Object lock = new Object();
private Form form = new Form("MIDlet2");
private Command okCmd = new Command("Show Value", Command.OK, 0);
public MIDlet2() {
form.addCommand(new Command("Exit", Command.EXIT, 0));
form.addCommand(okCmd);
form.setCommandListener(this);
instance = this;
} public void commandAction(Command cmd, Displayable arg1)
{
if (cmd.getCommandType() == Command.EXIT) {
notifyDestroyed();
} if (cmd == okCmd) {
form.append(" \ nvalue=" + value);
}
}
As you can see, when you press the Show value button, MIDlet2 prints
the integer member value . So far, not very exciting.
Because MIDlet1 and MIDlet2 run in the same memory space, MIDlet1
can change MIDlet2.value from its CommandListener.command-
Action() implementation:
public void commandAction(Command cmd, Displayable disp)
{
if (cmd.getCommandType() == Command.EXIT) {
notifyDestroyed();
} if (cmd == incCmd) {
// change MIDlet2.value member, from MIDlet1
MIDlet2.value++;
}
}
This is quite exciting but we can do something even more entertaining
and invoke methods in MIDlet2 from MIDlet1. For example, let's add to
MIDlet2 that, when a command is pressed, it waits on a thread and a
method that resumes the waiting thread:
Search WWH ::




Custom Search