Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an SMS is actually sent

Tags:

android

sms

In my SMS application I am sending SMSes using an SmsManager. After that I want to display a Toast saying "Message sent" or "Message not sent". How could I check if the message was actually sent? Could be like a no connection issue? or no SIM? How could I detect these?

like image 457
Ra Ghazi Avatar asked Sep 12 '13 18:09

Ra Ghazi


1 Answers

you can set listeners for both for delievery and sending my method is

PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,new Intent(DELIVERED), 0);
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";


// ---when the SMS has been sent---
        mContext.registerReceiver(
                new BroadcastReceiver()
                {    
                    @Override
                    public void onReceive(Context arg0,Intent arg1)
                    {
                        switch(getResultCode())
                        {
                    case Activity.RESULT_OK:

                            break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                            break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                            break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                            break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                            break;
                        }
                    }
                }, new IntentFilter(SENT));
        // ---when the SMS has been delivered---
        mContext.registerReceiver(
                new BroadcastReceiver()
                {

                    @Override
                    public void onReceive(Context arg0,Intent arg1)
                    {
                        switch(getResultCode())
                        {
                        case Activity.RESULT_OK:
                            break;
                        case Activity.RESULT_CANCELED:
                            break;
                        }
                    }
                }, new IntentFilter(DELIVERED));


SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null,message,sentPI, deliveredPI);
like image 56
Trikaldarshiii Avatar answered Sep 28 '22 21:09

Trikaldarshiii