SMS/Message delivery and sent confirmation in Android with example


          Before proceeding with this example, see how to send SMS in Android here.
             To track the transmission and delivery of outgoing SMS, implement  and register broadcast receivers that listen for actions you specify when creating the Pending Intents you pass in to the sendTextMessage method.In this example we will be making use of Intent, PendingIntent and Broadcastreceiver to track message delivery.

We will be reusing the same code, seen in the above example to send SMS.
To send  a text message use sendTextMessage from SMS Manager.

sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

The first Pending Intent parameter (sentIntent) is fired when the message is either successfully sent or failed. The result code for the Broadcast receiever that receives this Intent will be one of the following.

Activity.RESULT_OK - To indicate a successful transmission.
Sms.Manager.RESULT_ERROR_GENERIC_FAILURE - To indicate a nonspecific failure
Sms.Manager.RESULT_ERROR_RADIO_OFF - To indicate the phone radio is turned off
Sms.Manager.RESULT_ERROR_NULL_PDU - To indicate a PDU failure.
SmsManager.RESULT_ERROR_NO_SERVICE - To indicate that no cellular service is currently available.

The second Pending parameter(deliveryIntent) is fired only after the recipient receives your SMS message.

After sent and delivery events we have displayed appropriate messages through Toast.

Sent Confirmation

Delivery Confirmation
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
     >

 <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pn"
    android:textSize="15sp"    
    android:textColor="@android:color/white"
    android:background="@android:color/black"
    />

 <EditText
     android:id="@+id/phno"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@android:color/darker_gray"
     android:ems="10" >        
 </EditText>
 <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/msg"
    android:textSize="15sp"    
    android:textColor="@android:color/white"
    android:background="@android:color/black"
    />
  <EditText
        android:id="@+id/smstxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
          android:background="@android:color/darker_gray"
        android:lines="5"
        android:gravity="top" />  
 <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b1" /> 


   </LinearLayout>

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
 Button btnSend;
 EditText etPhoneNo;
 EditText etMsg;

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  etPhoneNo = (EditText) findViewById(R.id.phno);
  etMsg = (EditText) findViewById(R.id.smstxt);
  btnSend = (Button) findViewById(R.id.send);

   btnSend.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    String phoneNo = etPhoneNo.getText().toString();
    String msg = etMsg.getText().toString();
    try {

      String SENT = "sent";
     String DELIVERED = "delivered";

      Intent sentIntent = new Intent(SENT);
     /*Create Pending Intents*/
     PendingIntent sentPI = PendingIntent.getBroadcast(
       getApplicationContext(), 0, sentIntent,
       PendingIntent.FLAG_UPDATE_CURRENT);

      Intent deliveryIntent = new Intent(DELIVERED);

      PendingIntent deliverPI = PendingIntent.getBroadcast(
       getApplicationContext(), 0, deliveryIntent,
       PendingIntent.FLAG_UPDATE_CURRENT);
     /* Register for SMS send action */
     registerReceiver(new BroadcastReceiver() {

       @Override
      public void onReceive(Context context, Intent intent) {
       String result = "";

        switch (getResultCode()) {

        case Activity.RESULT_OK:
        result = "Transmission successful";
        break;
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
        result = "Transmission failed";
        break;
       case SmsManager.RESULT_ERROR_RADIO_OFF:
        result = "Radio off";
        break;
       case SmsManager.RESULT_ERROR_NULL_PDU:
        result = "No PDU defined";
        break;
       case SmsManager.RESULT_ERROR_NO_SERVICE:
        result = "No service";
        break;
       }

        Toast.makeText(getApplicationContext(), result,
         Toast.LENGTH_LONG).show();
      }

      }, new IntentFilter(SENT));
     /* Register for Delivery event */
     registerReceiver(new BroadcastReceiver() {

       @Override
      public void onReceive(Context context, Intent intent) {
       Toast.makeText(getApplicationContext(), "Deliverd",
         Toast.LENGTH_LONG).show();
      }

      }, new IntentFilter(DELIVERED));

      /*Send SMS*/
     SmsManager smsManager = SmsManager.getDefault();
     smsManager.sendTextMessage(phoneNo, null, msg, sentPI,
       deliverPI);
    } catch (Exception ex) {
     Toast.makeText(getApplicationContext(),
       ex.getMessage().toString(), Toast.LENGTH_LONG)
       .show();
     ex.printStackTrace();
    }
   }
  });

  }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

Download source code of this application
Messagedelivery.rar

Hope this helped ! Share your thoughts...






Reactions:

7 comments :

  1. Thanks for the tutorial,nice one ,But I cannot find anything about addition of permission to androidmanifest.xml

    ReplyDelete
  2. Nice Tutorial but Message is not sending to specified mobile number.

    ReplyDelete
  3. nice tutorial . thanks dude

    ReplyDelete
  4. nice ..
    but is it work if i leave the activity before i got deliver status.

    ReplyDelete
  5. nice work, but you should unregister the broadcast receiver in OnPause(), else it will give "leaked IntentReceiver" error.

    ReplyDelete