Categories: Java ME | MIDP 2.0 | Code Examples | WMA 2.0 (JSR-205) | S60 3rd Edition, Feature Pack 1
This page was last modified 06:47, 27 May 2008.
CS000978 - Sending a multipart MMS
From Forum Nokia Wiki
| ID | CS000978 | Creation date | May 27, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N95 8GB |
| Category | Java ME | Subcategory | WMA 2.0 (JSR-205) |
| Keywords (APIs, classes, methods, functions):
javax.wireless.messaging.MessageConnection, javax.wireless.messaging.MultipartMessage, javax.wireless.messaging.MessagePart, javax.microedition.io.Connector, java.io.InputStream. java.lang.Thread, java.io.IOException, java.io.InterruptedIOException, java.lang.IllegalArgumentException, java.lang.SecurityException, javax.wireless.messaging.SizeExceededException, javax.microedition.io.Connector.open(), javax.wireless.messaging.MessageConnection.newMessage(), javax.wireless.messaging.MultipartMessage.setAddress(), javax.wireless.messaging.MultipartMessage.setSubject(), javax.wireless.messaging.MultipartMessage.setHeader(), javax.wireless.messaging.MultipartMessage.addMessagePart(), javax.wireless.messaging.MessageConnection.send(), javax.wireless.messaging.MessageConnection.close() |
Overview
This code snippet demonstrates how to send a multipart MMS message. In the snippet, the user first enters a destination address to a text field. He or she may also fetch a phone number from the address book. After that, the user enters a subject for the message. Then, by selecting Send, an image is attached to the message and the message is sent to the specified address.
In the code, the following message parameters are set:
- Destination address (specified by the user)
- Subject of the message (specified by the user)
- Priority (set to normal)
- Message content (a JPEG image loaded from a resource).
This is a complete example MIDlet, but the most interesting methods (methods regarding multipart message handling) are handleSendCommand(), prepareMessage(), createMsgPart(), and sendMMS().
Source file
import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import javax.microedition.io.Connector; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.wireless.messaging.MessageConnection; import javax.wireless.messaging.MessagePart; import javax.wireless.messaging.MultipartMessage; import javax.wireless.messaging.SizeExceededException;
public class MMSMIDlet extends MIDlet implements CommandListener { private Command sendCommand; private Command exitCommand; private Form mainForm; private TextField mmsAddress; private TextField mmsSubject; private MessageConnection connection; /** * Constructor. Constructs the object and initializes displayables. */ public MMSMIDlet() { mainForm = new Form("MMS Example"); mmsAddress = new TextField("Address", null, 40, TextField.PHONENUMBER); mainForm.append(mmsAddress); mmsSubject = new TextField("Subject", null, 255, TextField.PLAIN); mainForm.append(mmsSubject); sendCommand = new Command("Send", Command.ITEM, 0); mainForm.addCommand(sendCommand); exitCommand = new Command("Exit", Command.EXIT, 0); mainForm.addCommand(exitCommand); mainForm.setCommandListener(this); } /** * From MIDlet. * Called when the MIDlet is started. */ public void startApp() { // The initial display is the main form Display.getDisplay(this).setCurrent(mainForm); } /** * From MIDlet. * Called to signal the MIDlet to enter the Paused state. */ public void pauseApp() { // No implementation required } /** * From MIDlet. * Called to signal the MIDlet to terminate. * @param unconditional whether the MIDlet has to be unconditionally * terminated */ public void destroyApp(boolean unconditional) { if (connection != null) { try { // Close the message connection connection.close(); } catch (IOException ex) { // TODO: Exception handling } } } /** * From CommandListener. * Called by the system to indicate that a command has been invoked on a * particular displayable. * @param command the command that was invoked * @param displayable the displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) { if (command == exitCommand) { // Exit the MIDlet destroyApp(true); notifyDestroyed(); } else if (command == sendCommand) { handleSendCommand(); } } private void handleSendCommand() { try { // Open the connection connection = (MessageConnection)Connector.open("mms://:MyApp"); } catch (IOException ex) { // TODO: Exception handling } MultipartMessage message = prepareMessage(); sendMMS(message); } /** * Sets the parameters for the multipart message. */ private MultipartMessage prepareMessage() { // Prepare the multipart message MultipartMessage message = (MultipartMessage)connection.newMessage( MessageConnection.MULTIPART_MESSAGE); // Set the destination address String address = "mms://" + mmsAddress.getString(); message.setAddress(address); // Set the subject String subject = mmsSubject.getString(); message.setSubject(subject); // Set the priority String priority = "normal"; // "high", "normal" or "low" message.setHeader("X-Mms-Priority", priority); // Set the message part try { MessagePart messagePart = createMsgPart(); message.addMessagePart(messagePart); } catch (SizeExceededException ex) { // TODO: Exception handling } catch (IOException ex) { // TODO: Exception handling } return message; } /** * Constructs a MessagePart which can be added to a MultipartMessage. * @return the constructed MessagePart * @throws javax.wireless.messaging.SizeExceededException if the contents * is larger than the available memory or supported size for the message * part * @throws java.io.IOException if the resource cannot be read */ private MessagePart createMsgPart() throws SizeExceededException, IOException { String imageContentID = "image01"; String imageContentLocation = "image.jpg"; String jpgMIME = "image/jpeg"; InputStream imageContent = getClass().getResourceAsStream( imageContentLocation); MessagePart messagePart = new MessagePart(imageContent, jpgMIME, imageContentID, imageContentLocation, null); return messagePart; } /** * Sends a multipart message. */ private void sendMMS(final MultipartMessage message) { // Send the message on its own thread of execution Thread messageThread = new Thread() { public void run() { try { connection.send(message); mainForm.append("Message sent."); } catch (InterruptedIOException ex) { // TODO: Exception (e.g. timeout) handling } catch (IOException ex) { // TODO: Exception (e.g. network failure) handling } catch (IllegalArgumentException ex) { // TODO: Exception (e.g. too big or otherwise invalid // message) handling } catch (SecurityException ex) { // TODO: Exception (e.g. insufficient permissions) handling } } }; messageThread.start(); } }
Postconditions
The MIDlet sends an MMS message to the address specified by the user. The message consists of the subject text (specified by the user) and an image (loaded from a resource).
See also
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Nokia SDK 1.3 MMS libraries full of function prototypes? | beatsmith | General Messaging | 1 | 2007-07-04 14:14 |
| J2ME MMS vs Native MMS application | alliance205 | Mobile Java Networking & Messaging & Security | 3 | 2006-12-19 03:30 |
| MMS data in Nokia Mobile Internet Toolkit 3.1 | liyuanmingAlien | General Messaging | 1 | 2002-09-03 10:44 |
| MMS application launching on device | amity | General Messaging | 1 | 2007-07-03 11:10 |
| Sample.mms | teddyjas | General Messaging | 1 | 2003-02-18 09:46 |

