Java Reference
In-Depth Information
As new text SMS messages arrive on the application's port,
c.receive
returns new
instances of
TextMessage
. The thread's
run
method extracts the payload text from the
incoming message and uses the
Display
instance's
callSerially
method to schedule an
update to the display on the main thread. It then loops to wait for another incoming
text message.
callSerially
is handy any time you need to schedule an operation on the applica-
tion's main thread from a separate thread. It schedules the
run
method of an instance of
the class you provide to run after the main thread has handled any pending UI events.
The thread uses
callSerially
to schedule the
run
method of an instance of the inner
class
SetMessage
, which simply sets the output text field's contents to the text of the
incoming message.
To send a message, SMSMIDlet invokes the
sendMsg
method of the
SMSSender
class.
This singleton class uses a separate thread to send each MO message to prevent blocking
the main thread. Listing 14-7 shows the
SMSSender
class.
Listing 14-7.
The SMSSender Class
package com.apress.rischpater.smsmidlet;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.IOException;
public class SMSSender implements Runnable {
private static SMSSender me = new SMSSender();
private String receiver = null;
private String port = null;
private String msgString = null;
private boolean sending = false;
private SMSSender() {
}
public static SMSSender getInstance() {
return me;
}
public boolean isSending() {
return sending;
}