Click here to Skip to main content
Click here to Skip to main content

Tagged as

Sending a SMS Message from an Android Application

, 22 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article I will discuss possible ways to send an SMS message from an Android application.

Introduction

We often come across situations where we are required to send a text message from our Android app. In this article we will explore all possible ways to achieve this simple yet very useful task.

Background

There are two possible ways to send a text message from an Android app
1. The first way is to send it programmatically from your application.
2. The second way is to send it by invoking the built-in SMS application.

In this article we will explore both the scenarios one by one.

* If you are new to Android app development, do refer to the Demo Project section of this article for some useful tips.

1. Sending a SMS programmatically from your application

Include the following permission in your AndroidManifest.xml file -

<uses-permission android:name="android.permission.SEND_SMS" />

Import the package -

import android.telephony.SmsManager;

Code to send a SMS -

public void sendSMS() {
    String phoneNumber = "0123456789";
    String message = "Hello World!";

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}

The method sendTextMessage of class SmsManager sends a text based SMS.

Method Detail

public void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

Details about the parameters that the method accepts can be found here.

If you use the code above you will able to send messages with length less than or equal to 160 characters only.

Code to send a long SMS -

public void sendLongSMS() {
 
    String phoneNumber = "0123456789";
    String message = "Hello World! Now we are going to demonstrate " + 
            "how to send a message with more than 160 characters from your Android application.";

    SmsManager smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(message); 
    smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}

The method sendMultipartTextMessage of class SmsManager sends a multi-part text based SMS

Method Detail

public void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)

Details about the parameters that the method accepts can be found here

Note

The method divideMessage of class SmsManager divides the message text into several smaller fragments of size 160 characters or less.

* Refer to the Points of Interest section of this article for a useful tip.

2. Send a SMS by invoking the built-in SMS application using Intents.

To invoke the SMS application via intents we have to do the following:
- Set the action to ACTION_VIEW
- Set the mime type to vnd.android-dir/mms-sms
- Add the text to send by adding an extra String with the key sms_body
- Add the phone number of the recipient to whom you wish to send the message by adding an extra String with the key address

Note:
- The last two steps are optional, if you don't wish to specify the message text or the recipients you can ignore these steps.
- If you wish to set multiple recipients use semi-colon ';' as the separator in the string passed as the address

Code to send a SMS using Intents

    
public void invokeSMSApp() {
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);

        smsIntent.putExtra("sms_body", "Hello World!"); 
        smsIntent.putExtra("address", "0123456789");
        smsIntent.setType("vnd.android-dir/mms-sms");

        startActivity(smsIntent);
}

Points of Interest

If you choose to send messages programmatically and wish to add the message sent from your application in the native 'Messages' application of Android in the 'Sent' folder, here is the code to achieve it using Content providers -

Code to save a SMS in 'Sent' folder of native 'Messages' application

Include the following permission in your AndroidManifest.xml file -

<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

You will have to add both WRITE_SMS and READ_SMS permissions.

Add the following imports -

    import android.net.Uri;
    import android.content.ContentValues;

Insert the below code where you wish to perform the operation -

    ContentValues values = new ContentValues(); 
              
    values.put("address", "0123456789"); 
              
    values.put("body", "Hello World!"); 
              
    getContentResolver().insert(Uri.parse("content://sms/sent"), values);

Demo Project

I have created a demo project which implements all the scenarios as discussed in this article.

I have used EditText widget for the Phone Number and Message input. Here I'd like to mention two XML attributes that I have used -

  • android:hint This string is displayed as a hint to the user when the field is empty. This will give your application a more native look and feel.
  • android:inputType="phone" (for Phone Number field) This signals the input method (IME) that this field should accept only valid Phone Numbers. This will save you from validating user input.

Which way to go....

Both the ways have their own set of pros and cons. If you decide to use Intents to send text messages from your application then no additional permissions are required but it will become a two step process, for instance if the user presses any button in your app to send SMS the intent will be displayed where he/she will have to press send; whereas if you decide to do it programitically you might also have to check for the result.

Hope this small article helps someone out there.

Happy Coding!

History

- September 21, 2012; First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

gupta.avinash
Architect
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionNice Tutorial PinmemberMember 110292781-Oct-14 0:05 
GeneralRe: Nice Tutorial Pinprofessionalgupta.avinash1-Oct-14 0:14 
GeneralThanks for Help Pinmemberdev@androdeveloper.com14-Sep-14 22:33 
GeneralRe: Thanks for Help Pinmembergupta.avinash15-Sep-14 0:59 
QuestionThanks PinmemberAlain Peralta23-Jul-14 14:06 
QuestionBirthday wishing app PinmemberMember 106029671-Apr-14 5:46 
Questionstore string as variable PinmemberMember 1068420619-Mar-14 17:46 
QuestionGet Delivery of the message PinmemberMember 1067871218-Mar-14 0:03 
AnswerRe: Get Delivery of the message Pinmembergupta.avinash19-Mar-14 23:55 
QuestionFor My Application [modified] PinmemberMJR4PJCT16-Mar-14 15:12 
Questionabout android PinmemberMember 1056265131-Jan-14 2:43 
GeneralMy vote of 4 PinmemberMember 1056197330-Jan-14 19:55 
QuestionNIce tutorial... PinmemberAravinth A31-Dec-13 2:02 
QuestionR Can not resolved to a variable PinmemberHussein39928-Dec-13 7:51 
AnswerRe: R Can not resolved to a variable Pinmembergupta.avinash28-Dec-13 20:32 
GeneralTHANKS FOR HELP PinmemberAhishekh Tomar5-Oct-13 8:46 
QuestionReceiving a SMS from android Application PinmemberMember 1023189527-Aug-13 2:59 
QuestionDelivery Report after sending message PinmemberMember 1016730322-Jul-13 6:48 
AnswerRe: Delivery Report after sending message Pinmemberdave ztong21-Aug-13 22:54 
GeneralHi Avinash, PinmemberMember 991086811-Jul-13 22:42 
QuestionNice Post!! Great Pinmemberboss prabu18-Jun-13 9:22 
QuestionThanks Pinmemberzaher_khdir3-Jun-13 1:19 
Questionhi PinmemberRawan Abu Wardeh4-Mar-13 13:16 
GeneralMy vote of 5 Pinmember| B | A | N | D | I |16-Feb-13 1:26 
QuestionRegarding sending the sms to our mobile phones PinmemberDeepak S. Amane13-Feb-13 5:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 22 Sep 2012
Article Copyright 2012 by gupta.avinash
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid