Java Reference
In-Depth Information
finally {
if (c != null) {
try {
c.close();
} catch (IOException e) {}
}
}
}
}
Identical in structure to the
SMSSender
class,
MMSSender
must do two things differently.
First, it must determine the character encoding scheme used by the host Java ME virtual
machine, so it can send it along with any outgoing MMS messages that include a text
part. Second, it must manage multiple message parts. Although the user interface for
MMSMIDlet only supports displaying the image part of an incoming MMS message, the
MMSSender
can send both a text part and an image part. By sending both a text part and an
image part, I show you how to send multiple parts in a single
MultipartMessage
instance
and thoroughly test the implementation of the MIDlet's message receipt functionality.
Getting the encoding scheme that the host virtual machine uses is easy; the
sendMsg
method just invokes the
System.getProperty
method with the value
microedition.
encoding
. This returns the standard name of the encoding scheme, which you can then
use when sending text parts of MMS messages. Once the
sendMsg
method captures its
incoming arguments, it creates and starts a new thread to send the outgoing message
contents.
The thread uses the private
sendMMS
method to actually perform the sending; this
method must create
MessagePart
instances for each of the indicated payloads, add those
parts to a new
MultipartMessage
instance, and send the
MultipartMessage
. For each of the
text and image parts, the method first obtains an array of bytes representing the payload.
For an image payload, it fetches the bytes from the resource in the MIDlet JAR file; for a
string payload, it fetches the bytes from the
String
containing the message. Then, it cre-
ates a new
MessagePart
instance, specifying the MIME type and a name for the part when
it creates the part. When creating the
MessagePart
, the
sendMMS
method doesn't specify a
location for the part, because there is no location. In addition, the
MessagePart
instance
that contains the text of the message to send also includes the encoding originally
fetched from the Java ME runtime. Once
sendMMS
creates the
MessagePart
instance with
the desired data, it adds the message part to the outgoing message. With both message
parts added to the outgoing message, the code uses the
MessageConnection
's
send
method
to send the message, just as it would any other WMA message.