Java Reference
In-Depth Information
memory, and it creates a new MessagePart consisting of the PNG image's contents. Finally,
it sends the image using the MessageConnection .
Receiving Messages
To receive a message, you need a MessageConnection instance configured to accept MT
messages. You create one using Connector.open , passing the protocol and optional port or
application ID for the kind of MT message your application is to receive, like this:
MessageConnection c = (MessageConnection)Connector.open("sms://:" + port);
You can then receive new messages by invoking the MessageConnection 's receive
function, like this:
TextMessage msg = (TextMessage)c.receive();
Sounds simple, doesn't it? There's one catch: receive blocks until the application
receives a message, so if you do this on the default thread, your application UI will hang
until an incoming message arrives. You can solve this problem in one of two ways: regis-
ter a listener that the WMA notifies when a message is available, or invoke receive on a
separate thread.
Registering a listener is easy; like other Java listeners, you simply create a class that
implements the appropriate listener interface (in this case, MessageListener ) and provide
the notifyIncomingMessage method. When the device receives a message, the AMS invokes
the listener with a MessageConnection configured to receive the incoming message.
The most robust method is actually to both register a listener and invoke receive
when the WMA invokes your listener, because receive can block for a short period while
the Java ME runtime and WMA process an incoming message once it arrives. I show you
this technique later in this chapter in the “Sending and Receiving MMS Messages” section.
Of course, you can only receive an incoming message this way while your application
is running. To receive an incoming message when your application is not running, your
application must register itself with the push registry, kept by the AMS. In turn, the AMS
invokes your application when the device receives a message for your application. I show
you how to do this in the upcoming section, “Using the Push Registry.”
Managing Message Headers
MulitpartMessage instances provide a collection of headers , which are name-value pairs
that intermediate servers may add to a message as it works its way from origination point
to destination. You can get the value of a message header by passing the name of the
header to getHeader , and you can set a header's value by passing the name of the header
and the value to setHeader . With HTTP, you can create your own headers and include
application-specific data. However, the MMS protocol that MultipartMessage represents
 
Search WWH ::




Custom Search