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:21, 27 May 2008.
CS000976 - Sending a text SMS
From Forum Nokia Wiki
| ID | CS000976 | 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.TextMessage, javax.microedition.io.Connector, java.lang.Thread, java.io.IOException, java.io.InterruptedIOException, java.lang.IllegalArgumentException, java.lang.SecurityException, javax.microedition.io.Connector.open(), javax.wireless.messaging.MessageConnection.newMessage(), javax.wireless.messaging.Message.setAddress(), javax.wireless.messaging.TextMessage.setPayloadText(), javax.wireless.messaging.MessageConnection.send(), javax.wireless.messaging.MessageConnection.close() |
Overview
This code snippet demonstrates how to send a text SMS. In the snippet, the user first enters a phone number to a text field. He or she may also fetch the number from the address book. After that, the user enters the text to be sent. Then, by selecting Send, the SMS is sent to the specified number.
This is a complete example MIDlet, but the most interesting methods (methods regarding SMS handling) are handleSendCommand(), prepareSMS(), and sendSMS().
Source file
import java.io.IOException; 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.TextMessage;
public class SMSMIDlet extends MIDlet implements CommandListener { private Command sendCommand; private Command exitCommand; private Form mainForm; private TextField smsNumber; private TextField smsText; private MessageConnection connection; /** * Constructor. Constructs the object and initializes displayables. */ public SMSMIDlet() { mainForm = new Form("SMS Example"); smsNumber = new TextField("Phone number", null, 20, TextField.PHONENUMBER); mainForm.append(smsNumber); smsText = new TextField("Text", null, 160, TextField.PLAIN); mainForm.append(smsText); 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("sms://:5000"); } catch (IOException ex) { // TODO: Exception handling } TextMessage message = prepareSMS(); sendSMS(message); } /** * Sets the destination address and payload text for the text SMS. */ private TextMessage prepareSMS() { // Prepare the text message TextMessage message = (TextMessage)connection.newMessage( MessageConnection.TEXT_MESSAGE); // Set the destination address String number = "sms://" + smsNumber.getString(); message.setAddress(number); // Obtain the specified text and set it as the payload String text = smsText.getString(); message.setPayloadText(text); return message; } /** * Sends a text SMS. */ private void sendSMS(final TextMessage message) { // Send the message on its own thread of execution Thread smsThread = 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 } } }; smsThread.start(); } }
Postconditions
The MIDlet sends a text SMS to the number specified by the user.
See also
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sending SMS | philg001 | General Messaging | 0 | 2008-03-03 00:27 |
| How to send & Recive Unicode SMS using Nokia PC COnnectivity SDK | naveen_galipally | PC Suite API and PC Connectivity SDK | 1 | 2002-09-19 01:36 |
| Help needed : sending long ringtones with multiple SMS messages | jlmfr | Smart Messaging | 5 | 2002-09-18 08:10 |
| security aspects in sending sms? | kumar_lena | General Messaging | 1 | 2004-02-18 12:45 |
| j2me:basics of sending free sms | smart@gayu | General Messaging | 4 | 2007-04-05 10:18 |

