How to send an SMS on the Android API

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to send an SMS on the Android API
Android apps can facilitate using telephony functions from within their own user interfaces. (Courtesy of Android)

The Android operating system runs on mobile computing devices such as smartphones. Android application programmers can utilise various aspects of these mobile devices, such as telephony functions. With the telephony resources in the Android APIs (Application Programming Interfaces), developers can write apps in which the user can send SMS text messages. If you are writing an Android app and wish to allow the user to send a text message, you can use the telephony classes directly within your Java code, sending an SMS with only a few lines of code.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Alter your application Manifest. To send SMS text messages, your app must have a particular permission. This will allow anyone downloading the app to see clearly that it has the facility to send SMS. Open your application Manifest file XML code. Add the following permission element:

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

    Make sure you include the permission inside the manifest element but outside any other elements. Without this permission, the app will be unable to send text messages.

  2. 2

    Add the telephony resources to your app. Open the Activity or other class you plan on sending the text message from. At the top of the class file, add the following import statement:

    import android.telephony.SmsManager;

    Create and instantiate an SMS Manager object at the point in your class where you wish to use SMS functionality:

    SmsManager smsMan = SmsManager.getDefault();

    This object will allow you to send text messages.

  3. 3

    Configure the details of your SMS. The SMS Manager class provides methods for sending text messages. You pass the details of the message you want to send when you call these methods. You can include the address to send the message to, which can be a telephone number, the source address, which is the service centre address, the text content of the message, an Intent to broadcast when the message is sent and an Intent to broadcast when it is delivered. In many cases you will not need all of these details. The following sample code demonstrates creating variables for the recipient number and the text message content:

    String recipientNum = "01234567890"; String textMessage = "Hi! How are you?";

  4. 4

    Send the message. You can call the SMS Manager methods to send the message, passing the message details at the same time. The following example demonstrates:

    smsMan.sendTextMessage(recipientNum, null, textMessage, null, null);

    This code passes the recipient number and message content, leaving the source address and two Intents for when the message is sent or received null, as these are typically not required.

Tips and warnings

  • You can allow users to share content to an application of their choice using the "ACTION_SEND" Intent rather than sending exclusively to the text messaging service.
  • The appearance of the SMS sending permission may stop some users from downloading your app if sending text messages is not a vital part of the app's functionality.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.