Recipe 5.3 Monitoring the Status of Sent SMS Messages Programmatically

Android Versions
Level 4 and above
Permissions
android.permission.SEND_SMS
Source Code to Download from Wrox.com
SendSMSWithFeedback.zip

When sending SMS messages programmatically, you might want to monitor the sending status to ensure that the messages are sent and delivered correctly.

Solution

To monitor the status of the SMS message sending, you need to make use of two PendingIntent objects: one for tracking when the message is sent, and another one for tracking whether the message is delivered correctly. You also need two BroadcastReceiver objects, so that you can listen for intents when the SMS message has been sent and delivered:

package net.learn2develop.sendsmswithfeedback;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.os.Bundle;

public class MainActivity extends Activity {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI, deliveredPI;
    BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}
NOTE What is the difference between a sent message versus a delivered message? A sent message means that the message has been sent successfully from the sender’s phone; but it may or may not be delivered to the recipient. For example, a message ...

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.