Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 03:39, 24 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to send text SMS in Java ME

From Wiki
Jump to: navigation, search

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.

Article Metadata
Code ExampleArticle
Created: SergioEstevao (15 Nov 2007)
Last edited: hamishwillee (24 Jul 2013)

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 splitt 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

This page was last modified on 24 July 2013, at 03:39.
279 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×