Java Reference
In-Depth Information
2.10.2 Receiving Messages
Receiving a message is, again, straightforward:
MessageConnection smsconn = null;
Message msg = null;
String receivedMessage = null;
String senderAddress = null;
try
{
conn = (MessageConnection) Connector.open(("sms://:1234");
msg = smsconn.receive();
...
// get sender's address for replying
senderAddress = msg.getAddress();
if (msg instanceof TextMessage)
{
// extract text message
receivedMessage = ((TextMessage)msg).getPayloadText();
// do something with message
...
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
We open a server mode
MessageConnection
by passing in a URL
of the following syntax:
sms://:1234
We retrieve the message by invoking the following method on the
MessageConnection
instance.
public Message receive()
The address of the message sender can be obtained using the following
method of the
Message
interface:
public String getAddress()
A server mode connection can be used to reply to incoming messages,
by making use of the
setAddress()
method of the
Message
interface.
In the case of a text message, we cast the
Message
object appropriately
and then retrieve its contents with the
TextMessage
interface, using the
method below:
public String getPayloadText()