15

I developed codes to send SMS to more than one persons. But current problem is that I cannot know which person is got the message and which is sent failed. I want to know each of sent SMS status and the receiver phone number, is it can be done?

user429079
  • 203
  • 3
  • 8
  • I successfully implemented this in my app.. Please check - http://stackoverflow.com/questions/16388236/not-able-to-pass-retrieve-extras-inside-broadcast-receiver – mboy May 06 '13 at 10:38

1 Answers1

12

Here is the code snippet you are looking for ...

//---sends an SMS message to another device---

private void sendSMS(String phoneNumber, String message)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            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;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
}
Mathias Conradt
  • 28,117
  • 20
  • 135
  • 191
Muhammad Shahab
  • 5,255
  • 4
  • 36
  • 47
  • I would also recommend this website ... http://mobiforge.com/developing/story/sms-messaging-android – Muhammad Shahab Jan 09 '11 at 15:48
  • 4
    if you're sending multiple sms in a loop at the same time, how do you identify which SMS the broadcast is for? What's the unique identifier? – Mathias Conradt May 06 '11 at 07:18
  • @MathiasLin you got any solution over this ? – Hunt Jun 18 '12 at 09:11
  • 1
    @MathiasLin, you can identify the SMS in the Intent extras of the broadcast Intent: Intent intent = new Intent(SENT); intent.putExtra(KEY, identifier);. You will find that data in the intent received by the broadcast receiver (called arg1 above). – andyandy Sep 11 '13 at 17:45
  • This is outdated now. Since 4.4, `only the default SMS app receives the SMS_DELIVER_ACTION broadcast` . See https://developer.android.com/about/versions/android-4.4.html#SMS – aandis Mar 28 '15 at 08:52
  • 2
    PendingIntent sentPI = PendingIntent.getBroadcast(this, requestCode, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, requestCode, new Intent(DELIVERED), 0); Note: put a different requestCode for each sms. – Massimo Fazzolari Jan 24 '17 at 16:00
  • how can i findout delivery comes from what number ?! added more data to delivery that get it in DeliverReceiver . – alireza 2 mins ago edit – alireza Jan 29 '18 at 06:31