NAVIGATION - SEARCH

Android - How To Send SMS Messages Programmatically And Monitor Its Delivery

There are scenarios when you would want to send SMS messages programmatically from your Android app. And also you would want to get notified if the message was delivered successfully. A typical scenario can be if you want to build an app that can be used to send bulk SMS messages during some festive occasion like New Year eve. Android gives you built in functionality to send out SMS messages programmatically.

To send an SMS message programmatically from within your Android app, use the SmsManager class, available from the android.telephony.SmsManager package. Here we are assuming you have an EditText to collect phone number and a Button to send the SMS message. To monitor the SMS message sent status and delivery status, we make use of a BroadcastReceiver object and PendingIntent object to get notified.

A message is not delivered if the recipient phone is switched off or the carrier is not able to deliver the message to the recipient.

In the following code we are following these steps to successfully send SMS message and get the sent and delivery status.

1. Declare 2 PendingIntent objects which gets broadcasted on successfull sent and delivery
2. Register 2 BroadcastReceiver objects which gets notified on successfull sent and delivery
3. You should register BroadcastReceiver in onResume and un-register in onPause so that if your app gets killed or is not active then broadcast receivers are removed
4. Display the status as Toast

public class MainActivity extends Activity 
{
	String INTENT_SMS_SENT = "INTENT_SMS_SENT";
	String INTENT_SMS_DELIVERED = "INTENT_SMS_DELIVERED";
	
	PendingIntent intentSENT, intenDELIVERY;
	BroadcastReceiver broadcastSENT, broadcastDELIVERY;

	EditText txtPhone;
	Button btnSend;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txtPhone = (EditText) findViewById(R.id.txtPhone);
		btnSend = (Button) findViewById(R.id.btnSend);
		
		btnSend.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				SmsManager sms = SmsManager.getDefault();
				sms.sendTextMessage(txtPhone.getText().toString().trim(), null, "Hello Message Text", intentSENT, intenDELIVERY);
			}
		});

		intentSENT = PendingIntent.getBroadcast(this, 0, new Intent(INTENT_SMS_SENT), 0);
		intenDELIVERY = PendingIntent.getBroadcast(this, 0, new Intent(INTENT_SMS_DELIVERED), 0);
	}

	@Override
	public void onResume() 
	{
		super.onResume();
		
		broadcastSENT = new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context arg0, Intent arg1) 
			{
				switch (getResultCode())
				{
					case Activity.RESULT_OK:
						Toast.makeText(getBaseContext(), "SMS sent successfully", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
						Toast.makeText(getBaseContext(), "Generic failure occurred", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_NO_SERVICE:
						Toast.makeText(getBaseContext(), "No service available", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_NULL_PDU:
						Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
						break;
					case SmsManager.RESULT_ERROR_RADIO_OFF:
						Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
						break;
				}
			}
		};
		
		broadcastDELIVERY = new BroadcastReceiver()
		{
			@Override
			public void onReceive(Context arg0, Intent arg1) 
			{
				switch (getResultCode())
				{
					case Activity.RESULT_OK:
						Toast.makeText(getBaseContext(), "SMS delivered successfully", Toast.LENGTH_SHORT).show();
						break;
					case Activity.RESULT_CANCELED:
						Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
						break;
				}
			}
		};

		registerReceiver(broadcastSENT, new IntentFilter(INTENT_SMS_SENT));
		registerReceiver(broadcastDELIVERY, new IntentFilter(INTENT_SMS_DELIVERED));
	}

	@Override
	public void onPause() 
	{
		super.onPause();

		unregisterReceiver(broadcastSENT);
		unregisterReceiver(broadcastDELIVERY);
	}
}

You need to add the following permissions in your AndroidManifest.xml file

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

You are free to modify this code and build your own logic as per your requirements.

Add comment