Java Reference
In-Depth Information
} catch (Exception e) {}
}
}
private void getMessage() {
synchronized(monitor) {
msgAvail++;
monitor.notify();
}
}
public void notifyIncomingMessage(MessageConnection c) {
if (c != null) {
getMessage();
}
}
class SetImage implements Runnable {
private Image img = null;
public SetImage(Image i) {
img=i;
}
public void run() {
imageItem.setImage(img);
}
}
}
The division of responsibility in the
MMSMIDlet
class is the same as in the
SMSMIDlet
class, although both the receiving and sending code is more complicated, reflecting
the asynchronous nature of MMS interaction. Initialization for message receipt in
startReceive
is similar to the implementation in
SMSMIDlet
, with one exception: the
MMSMIDlet
implementation registers a message listener with the
MessageConnection
returned from the GCF, so that the Java ME runtime can notify it of incoming messages
through the
notifyIncomingMessage
method.
When the MIDlet receives notification of an incoming message, it invokes
getMessage
. Together,
getMessage
and the receive thread's
run
method (originally
spawned by
startReceive
) use a message counter
msgAvail
and the monitor variable
monitor
to coordinate message receipt; if the MIDlet is receiving a message,
getMessage
waits before notifying the receive thread that another incoming message is pending.
The receive thread's main loop is slightly more complicated because it uses the
monitor; it first waits on the monitor and then receives the incoming message. Receiv-
ing the message is the same as in the SMSMIDlet example—it just invokes
receive
on