Sending SMS Messages Programmatically in Your Android Application

Recipe 5.2 Sending SMS Messages Programmatically in YourAndroid Application Development Cookbook: 93 Recipes for Building Winning Apps Android Application


Android Versions

Level 4 and above

Permissions

android.permission.SEND_SMS

Source Code to Download from Wrox.com


In some situations you might want to programmatically send SMS messages in your Android application. For example, your application might monitor the current location of a user and automatically send an SMS message to a predefined phone number, which would be very helpful if you were building an application to ensure someone’s safety, such as children or the elderly at home.

 

Solution

To send an SMS message programmatically from within your Android application, use the SmsManager class, available from the android.telephony.SmsManager package. The following code snippet shows how to send an SMS message to another device (emulator 5556) with the message “Greetings!”:


NOTE To enable sending SMS messages programmatically from your application, be sure to add the android.permission.SEND_SMS permission; otherwise, your application will crash.

package net.learn2develop.messagingapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = new Intent(android.content.Intent.ACTION_VIEW);
        i.putExtra("address", "5556; 5558; 5560");
        i.putExtra("sms_body", "Greetings!");
        i.setType("vnd.android-dir/mms-sms");
        startActivity(i);
    }

}

You first create an instance of the SmsManager class using the SmsManager.getDefault() method, and then call the sendTextMessages() method to send an SMS message.

The sendTextMessages() method takes five arguments:

  • destinationAddress — Phone number of the recipient
  • scAddress — Service center address; use null for default SMSC
  • text — Content of the SMS message
  • sentIntent — Pending intent to invoke when the message is sent (discussed in more detail in the next recipe)
  • deliveryIntent — Pending intent to invoke when the message has been delivered (discussed in more detail in the next recipe)


NOTE If you send an SMS message programmatically using the SmsManager class, the sent message will not appear in the built-in Messaging application.

This article is excerpted from chapter 5 "Messaging" of the Wrox book "Android Application Development Cookbook: 93 Recipes for Building Winning Apps" (ISBN: 978-1-1181-7767-9, copyright 2013, John Wiley & Sons) Wei-Meng Lee Wei-Meng Lee is the founder of Developer Learning Solutions (www.learn2develop.net), a technology company specializing in hands-on training in the latest Microsoft and Apple technologies. He writes extensively for online publications and is also the author of Beginning Android 4 Application Development and Beginning iOS 5 Application Development

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *