This is the main class, and it controls the state changes between screens.
It also owns the message connection and registers itself as a message listener
waiting for incoming messages. When sending a message, the sendMessage
method
will launch a new thread that will take care of sending the message. The application
ID used for the connection in both ends is also defined here.
Create the MMSMIDlet
class
file.
Import the required classes.
package mmsmidlet; import java.io.*; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.wireless.messaging.*;
Set MMSMIDlet
to
extend MIDlet
and implement MessageListener
.
Define the constants and classes used by the application and create the constructor.
The MessageListener
interface provides a mechanism for
the application to be notified of incoming messages. The MessageConnection
interface
defines the basic functionality for sending and receiving messages. It contains
methods for sending and receiving messages, factory methods to create a new Message
object,
and a method that calculates the number of segments of the underlying protocol
that are needed to send a specified Message
object. The Message
is
the base interface for derived interfaces that represent various types of
messages.
For
more information, see MessageListener
, MessageConnection
, and Message
in
the WMAPI 2.0 specification.
// Main MIDlet class. It controls the user interface and the // MMS connection public class MMSMIDlet extends MIDlet implements MessageListener { private final String APPLICATION_ID = "mmsdemo"; private CameraScreen cameraScreen = null; private ReceiveScreen receiveScreen; private SendScreen sendScreen; private InfoScreen infoScreen; private Displayable resumeDisplay = null; private MessageConnection messageConnection; private boolean closing; private Message nextMessage = null; public MMSMIDlet() {}
Create the three required MIDlet methods detailed below.
public void startApp() { if (resumeDisplay == null) { // Start the MMS connection startConnection(this); // Create the user interface cameraScreen = new CameraScreen(this); infoScreen = new InfoScreen(); sendScreen = new SendScreen(this); Display.getDisplay(this).setCurrent(cameraScreen); resumeDisplay = cameraScreen; cameraScreen.start(); } else { Display.getDisplay(this).setCurrent(resumeDisplay); } } public void pauseApp() { if (Display.getDisplay(this).getCurrent() == cameraScreen) { cameraScreen.stop(); } } public void destroyApp(boolean unconditional) { if (Display.getDisplay(this).getCurrent() == cameraScreen) { cameraScreen.stop(); } }
Create a method for exiting the application.
void exitApplication() { closeConnection(); destroyApp(false); notifyDestroyed(); }
Create a method for receiving incoming messages.
private synchronized void receive(Message incomingMessage) { if (receiveScreen==null) { receiveScreen = new ReceiveScreen(this); } receiveScreen.setMessage(incomingMessage); Display.getDisplay(this).setCurrent(receiveScreen); }
Create a callback method for incoming messages, which starts a new thread for receiving the message.
public void notifyIncomingMessage(MessageConnection conn) { // Callback for inbound message. // Start a new thread to receive the message. new Thread() { public void run() { try { Message incomingMessage = messageConnection.receive(); // this may be called multiple times if // multiple messages arrive simultaneously if (incomingMessage!=null) { receive(incomingMessage); } } catch (IOException ioe) { showError("Exception while receiving message: " + ioe.getMessage()); } } }.start(); }
Create a method
sending a message. Instances of the MessagePart
class
can be added to a MultipartMessage
. MultipartMessage
is
an interface representing a multipart message. The newMessage
method
constructs a new message object of a given type.
The setAddress
method
sets the "to" address associated with this message. The addMessagePart
method
attaches a MessagePart
to the multipart message.
For
more information, see MessagePart
, MultipartMessage
, newMessage
,
and addMessagePart
in the WMAPI 2.0 specification.
void sendMessage(String recipientAddress, MessagePart imagePart, MessagePart textPart) { try { // The MMS message is constructed here. // It is a multipart message consisting of two parts: // image and text. MultipartMessage mmsMessage = (MultipartMessage) messageConnection .newMessage(MessageConnection.MULTIPART_MESSAGE); mmsMessage.setAddress(recipientAddress); mmsMessage.addMessagePart(imagePart); mmsMessage.addMessagePart(textPart); nextMessage= mmsMessage; // Send the message in another thread new Thread() { public void run() { try { messageConnection.send(nextMessage); } catch (IOException ioe) { showError("Exception while sending message: " + ioe.getMessage()); } } }.start(); } catch (SizeExceededException see) { showError("Message size is too big."); } catch (IllegalArgumentException iae) { showError("Phone number is missing."); } }
Create a method for fetching the application ID.
// return the application id, either from the // jad file or from a hardcoded value String getApplicationID() { String applicationID = this.getAppProperty("Application-ID"); return applicationID==null?APPLICATION_ID:applicationID; }
Create the methods for showing the various screens and displays used by the application.
// Upon capturing an image, show the compose screen void imageCaptured(byte[] imageData) { cameraScreen.stop(); resumeDisplay = sendScreen; Display.getDisplay(this).setCurrent(sendScreen); sendScreen.initializeComposeCanvas(imageData); } // Shows the screen capture camera void showCameraScreen() { resumeDisplay = cameraScreen; Display.getDisplay(this).setCurrent(cameraScreen); cameraScreen.start(); } // Shows the incoming message screen void showReceiveScreen() { resumeDisplay = receiveScreen; Display.getDisplay(this).setCurrent(receiveScreen); } void showSendScreen() { resumeDisplay = sendScreen; Display.getDisplay(this).setCurrent(sendScreen); } void resumeDisplay() { Display.getDisplay(this).setCurrent(resumeDisplay); } // Displays the info screen void showInfo(String messageString) { infoScreen.showInfo(messageString, Display.getDisplay(this) ); } // Displays the error screen void showError(String messageString) { infoScreen.showError(messageString, Display.getDisplay(this) ); }
Create methods
for starting and closing the message connection. The setMessageListener
method
registers a MessageListener
object that the platform
can notify when a message has been received on this MessageConnection
.
For more
information, see setMessageListener
in the WMAPI 2.0 specification.
// Closes the message connection when the application // is stopped private void closeConnection() { closing = true; if (messageConnection != null) { try { messageConnection.close(); } catch (IOException ioe) { // Ignore errors on shutdown } } } // Starts the message connection object private void startConnection(final MMSMIDlet mmsmidlet) { if (messageConnection == null) { // Open connection in a new thread so that it doesn't // block if a security permission request is shown new Thread() { public void run() { try { String mmsConnection = "mms://:" + getApplicationID(); messageConnection = (MessageConnection) Connector.open(mmsConnection); messageConnection.setMessageListener(mmsmidlet); } catch (IOException ioe) { showError("Exception while opening message connection: " + ioe.getMessage() ); } } }.start(); } } }