Java Reference
In-Depth Information
public void sendMsg(String r, String p, String m) {
if (sending) return;
receiver = r;
port = p;
msgString = m;
Thread t = new Thread(this);
t.start();
}
public void run() {
String address = "sms://" + receiver + ":" + port;
sending = true;
MessageConnection c = null;
try {
c = (MessageConnection) Connector.open(address);
TextMessage txtmessage = (TextMessage) c.newMessage(
MessageConnection.TEXT_MESSAGE);
txtmessage.setAddress(address);
txtmessage.setPayloadText(msgString);
c.send(txtmessage);
} catch (Exception e) {}
if (c != null) {
try {
c.close();
} catch (IOException ioe) {}
}
sending = false;
}
}
By making the constructor private, saving an instance of the class in the private field
me , and returning that instance via the public method getInstance , the class enforces the
singleton relationship and ensures that clients of the class can obtain only a single
instance of the class.
The work this class does is divided into two methods: sendMsg and run . sendMsg sets
aside the parameters for the message it should send—the recipient address, recipient
port, and text of the message itself—before scheduling a new thread using the allocated
instance of SMSSender as the Runnable object.
Once the Java runtime starts the new thread, it invokes the run method, which
actually sends the text of the message to the address and port you provide. The process is
straightforward: create the destination address, use the GCF to create a MessageConnection
instance so you can obtain a new TextMessage instance, configure the TextMessage
 
Search WWH ::




Custom Search