Java Reference
In-Depth Information
Calling the Blocking receive() Method
The blocking receive() method is on the MessageConnection interface. This method will block
the incoming call until an incoming message is available or until the connection is closed. The
syntax for receive() is as follows:
public Message receive() throw IOException, InterruptedIOException
IOException will be thrown if an error is encountered during the receiving process, if the
connection is closed, or if the method is called on a connection opened in the client mode
(send only).
Since receive() is a blocking call, it should always be called on its own thread. A common
approach is to create a “receiver thread” to receive messages in an application.
InterruptedIOException is thrown if the connection is closed while the call is still blocking.
Closing a MessageConnection is a way to release a receiver thread that may be blocked waiting
for incoming messages.
Typical message receive code when using the blocking receive() looks like this:
conn = (MessageConnection) Connector.open("sms://5550001:1234");
msg = conn.receive(); // Blocking here
mSenderAddress = msg.getAddress(); // Get info from message
if (msg instanceof TextMessage) {
String msgReceived = ((TextMessage)msg).getPayloadText();
// Do something with the message here
} else if (msg instanceof BinaryMessage) {
byte [] msgReceived = ((BinaryMessage)msg).getPlayloadData();
// do something with the binary message here
}
}
When working with the payload of incoming messages, the following method on the
BinaryMessage interface is used to access the content of the binary message:
public byte [] getPayloadData();
On a TextMessage , the following method is used to access the content of the text message:
public String getPlayloadText();
Search WWH ::




Custom Search