Recipe 5.3 Monitoring the Status of Sent SMS Messages Programmatically
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);
}
}
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.