Send an SMS Text Message with Android

Sending an SMS text message with Android is a very important action when it comes to the Internet of Things(IoT). Sending SMS text messages is a great way to report data to a remote user. This may also be a great way to communicate with other cellular connected devices.

send sms text message android dwight
Image borrowed from http://worstfailever.com/best-ever/dwight-schrute-sms-the-office-quotes/

There are two main ways to send SMS text messages with the Android SDK. First is using intents, remember my Intro to Intents? Explaining them as messaging objects. The second method is using an SMS Manager object which I will not be covering during this tutorial. So let’s get started building our Intent to send a text message.

This tutorial assumes you know how to create a project with an empty activity. If you haven’t done that, please see my post on how to create your first project. The project name will be SMSTextSend and the activity name will be SMSTextHome.

Add the following method underneath the onCreate method in the SMSTextSend.java file.

    public void onClickSayHi() {
        Intent smsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + "8005551234"));
        smsIntent.putExtra("sms_body", "Hi!");
        startActivity(smsIntent);
    }

Next we’ll add a button to the layout calling the method we’ve just created. Open your activity_smstext_home.xml file and add the following within the RelativeLayout element.

    <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send away!" android:layout_centerVertical="true" android:onClick="onClickSayHi"/>

Launch the program and see what you get!

Why didn’t my text message send?

If you happen to have multiple messenger apps you’ll get a choice of which to send the message with. Select your messenger application and proceed. The message should be typed in for you and should be directed to the number you entered next to “sms:”.

The message hasn’t actually sent yet though. The problem is we’ve actually only sent the data to the messenger application. The user needs to interact with the application in order to send the message.

If you’re looking for a way to send SMS text messages without user interaction you’ll need to use the SMS Manager object. Check back soon for a tutorial on method 2!

Leave a Reply

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