×
Namespaces

Variants
Actions
(Difference between revisions)

How to send text SMS in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Remove unnecessary English translation link)
Line 1: Line 1:
[[Category:Java ME]][[Category:How To]][[Category:Messaging]][[Category:SMS]][[Category:WMA 1.0 (JSR-120)]][[Category:Code Snippet]]
+
[[Category:Java ME]][[Category:How To]][[Category:Messaging]][[Category:SMS]][[Category:WMA 1.0 (JSR-120)]][[Category:Code Snippet]][[Category:Code Examples]]
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= [[Media:HowToSendTextSMSMIDlet.zip]]
 
|sourcecode= [[Media:HowToSendTextSMSMIDlet.zip]]
Line 74: Line 74:
 
* [http://library.developer.nokia.com/index.jsp?topic=/Java_Developers_Library/GUID-71D2FC38-3A7C-4F69-9A2C-C307ABF33D6C/overview-summary.html WMAPI JavaDocs]
 
* [http://library.developer.nokia.com/index.jsp?topic=/Java_Developers_Library/GUID-71D2FC38-3A7C-4F69-9A2C-C307ABF33D6C/overview-summary.html WMAPI JavaDocs]
 
* [http://library.developer.nokia.com/index.jsp?topic=/Java_Developers_Library/GUID-5D55EA03-888E-437A-9498-82F606097B48_GUID-078BCC37-8746-4FF5-AD39-9FCB6C3F7F08.html Wireless messaging section on Java ME Developer's Library]
 
* [http://library.developer.nokia.com/index.jsp?topic=/Java_Developers_Library/GUID-5D55EA03-888E-437A-9498-82F606097B48_GUID-078BCC37-8746-4FF5-AD39-9FCB6C3F7F08.html Wireless messaging section on Java ME Developer's Library]
<!-- Translation --> [[en:How to Send Text SMS in Java ME]]
 
[[Category:Code Examples]]
 

Revision as of 02:49, 29 August 2012

Article Metadata
Code ExampleArticle
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

397 page views in the last 30 days.
×