Recipe 5.1 Sending SMS Messages Through the Built-in Messaging Application

Android Versions
Level 1 and above
Permissions
None
Source Code to Download from Wrox.com
MessagingApp.zip

In this recipe, you learn how to send SMS messages using the built-in Messaging application installed on all Android phones. Using this approach enables you to “send and forget” — all you need to do is invoke the Messaging application using an Intent object and pass it the necessary pieces of information, such as the recipient’s phone number and the message. Users see the familiar user interface of the Messaging application on their device, saving you the need to create a separate UI for entering the phone number, message, and so on.

Solution

To invoke the built-in Messaging application on your Android device to send an SMS message, you need to use an Intent object together with the startActivity() method. The following code shows how to invoke the built-in Messaging app:

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);
    }

}

Take ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.