×
Namespaces

Variants
Actions

How to send text SMS in Java ME

From Nokia Developer 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 06:39.
452 page views in the last 30 days.
×