Namespaces
Variants
Actions
Revision as of 02:49, 29 August 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to send text SMS in Java ME

Jump to: navigation, search
Article Metadata

Code Example
Article
Created: SergioEstevao (15 Nov 2007)
Last edited: hamishwillee (29 Aug 2012)


WMAPI allows Java ME applications to access messaging functionalities, as sending and receiving SMS and MMS messages. This article explains how to use it to send a simple text message.

Contents

Overview

The following Java ME tip explains a method of sending text messages using WMAPI. In the program TextMessage interface represents a text message. The setPayloadText() method sets the characters in the message.

JavaME HowToSendTextSMS.png

  public boolean sendSms(String number, String message){
boolean result = true;
try {
//sets address to send message
String addr = "sms://"+number;
// opens connection
MessageConnection conn = (MessageConnection) Connector.open(addr);
// prepares text message
TextMessage msg =
(TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set text
msg.setPayloadText(message);
// send message
conn.send(msg);
conn.close();
} catch (SecurityException se) {
// probably the user has not allowed to send sms
// you may want to handle this differently
result = false;
} catch (Exception e) {
result = false;
}
return result;
}

Download

You can download a sample MIDlet showing the code presented in this article here: Media:HowToSendTextSMSMIDlet.zip

Notes

  • Remember that a single message has a maximum size (140 bytes, which translates to 160 7-bit characters). If your message is too long to fit in one message it can be splitted to a maximum of three SMS, this is handled automatic by the WMA implementation.
  • Potentially-blocking operations, like sending a text message, should always be performed in separate threads, to avoid blocking the main MIDlet thread.

Related resources

414 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved