Java Reference
In-Depth Information
thread that invokes it until the device receives a new message, so you should definitely do
this on a thread different from the UI thread.
Sending Messages
Once you create a message, you need to set the data payload for the message. How you
do this differs slightly between the various
Message
subclasses. The
Message
class provides
methods only for managing the recipient address of the message and the time at which
the message was sent:
•
getAddress
: Returns the address associated with the message
•
getTimestamp
: Returns the time at which the message was sent as an instance of
java.util.Date
•
setAddress
: Associates a new address with the message, discarding any previous
address associated with the message
As I remarked previously in the section “Introducing Short Message Service,” the
implementation of SMS message packets can concatenate packets (called
segments
),
enabling you to send messages longer than the 160-character (140-byte) limitation of the
protocol. WMA requires that any implementation must support concatenating up to
three segments as a single message, letting you send reasonably long messages of 400
bytes or so (the actual length is generally a little shorter because of the space taken by the
protocol to support segment concatenation, an optional port number, and so forth). You can
determine how many segments a message consumes by invoking the
MessageConnection
's
numberOfSegments
method and passing a
TextMessage
or
BinaryMessage
; the result is the
number of segments the message spans.
The implementation of MMS is different, letting you create a message as a collection
of parts, represented by instances of
MessagePart
. Instead of setting a
MultipartMessage
payload all at once, you add individual parts.
Setting and Getting the Payload of a TextMessage
As you might imagine, managing the payload of a
TextMessage
instance is quite simple.
The class provides two methods:
getPayloadText
and
setPayloadText
. Each refers to the
message payload as an instance of
String
; you call
getPayloadText
to obtain a
String
containing the instance's payload, while you pass a string to
setPayloadText
to set the
instance's payload. You can set the payload of a text message using code similar to that in
Listing 14-1.