Java Reference
In-Depth Information
public void run() {
Message msg = null;
c = null;
endNow = false;
try {
c = (MessageConnection) Connector.open("sms://:" + port);
msg = c.receive();
while ((msg != null) && (!endNow)) {
if (msg instanceof TextMessage) {
msgReceived = ((TextMessage)msg).getPayloadText();
Display.getDisplay(this).callSerially(new SetMessage());
}
msg = c.receive();
}
} catch (IOException e) {}
}
class SetMessage implements Runnable {
public void run() {
msgEntry.setString(msgReceived);
}
}
}
The MIDlet uses two Command instances: one to signal the exit, and one to signal when
you select the send operation. It also uses two TextField instances: one for you to enter
the recipient MSISDN and one for the message you send or receive.
I based the implementation of this and the next sample application on the sample
code presented in the third edition of this topic, rather than building a new user interface
using NetBeans. Consequently, the flow of the user interface code is a little different;
these samples illustrate how you might hand-construct a MIDlet with its own application
instead of using NetBeans to create your UI. I create the Command instances at class cre-
ation time, and the remainder of the UI in the MIDlet's startApp method. This makes for
easy-to-read code in simple MIDlets, but doesn't scale well for large MIDlet applications
that use many different screens.
The MIDlet uses a second thread to receive incoming messages; it starts this thread
in the startApp method by invoking startReceive . This method simply allocates a new
Thread instance using the MIDlet itself as the implementation of the Runnable interface to
provide the thread's run method. I could have just as easily used an inner class, but that
seemed more work than it was worth. Consequently, as the MIDlet starts, the user inter-
face runs on the main thread, and a second thread starts and blocks in the run method on
the call to c.receive .
 
Search WWH ::




Custom Search