0

I am trying to receive the notification when my app sent sms successfully. I use BroadcastReceiver for the notification. I register in the onResume() and de-register in the onPause(). But it does not notify even though the sms is sent out. I set the break point in the BroadcastReceiver loop and does not have any notification. What could be the problem? My code is as follow.

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

    //---create the BroadcastReceiver when the SMS is sent---
    smsSentReceiver = 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;
            }
        }
    };

    //---create the BroadcastReceiver when the SMS is delivered---
    smsDeliveredReceiver = 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;
            }
        }
    }; 

    //---register the two BroadcastReceivers---
    registerReceiver(smsSentReceiver, new IntentFilter(SENT));
    registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));      
}

@Override
public void onPause() {
    super.onPause();
    //---unregister the two BroadcastReceivers---
    unregisterReceiver(smsSentReceiver);
    unregisterReceiver(smsDeliveredReceiver);        
}
Bryanyan
  • 677
  • 3
  • 13
  • 30
  • instead of onPause() method unregister the Receiver in onDestroy() , see this http://stackoverflow.com/q/4805269/2147039 – Raynold Mar 30 '13 at 11:19

1 Answers1

1

I am not sure about your use case. The documentation suggests that you unregister the receiver in onPause but you will miss the intent if it is sent when your app is in the background. If you want to receive the intent in background, you have 2 options:

  1. Have a service running and register the receiver there.
  2. If possible, you can register the broadcast receiver in the manifest.

The document says

Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead).

Also make sure that you don't have any typo in ACTION string ("android.provider.Telephony.SMS_RECEIVED") and have a correct permission ("android.permission.RECEIVE_SMS").

EDIT please see (especially CommonsWare's answer)

What is the Android sent sms intent?

Android Broadcast Receiver for Sent SMS messages?

"there is no Intent that is broadcast when Android sends an SMS. This is probably for privacy reasons as much as anything else."...CommonsWare

Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69
  • SMS_RECEIVED toast is working well. SMS_SEND toast does not work. SMS is sent out properly, just that the notification is not notified. I put a break point at public void onReceive(Context arg0, Intent arg1); but never hit the break point. – Bryanyan Mar 31 '13 at 04:01
  • Please see http://stackoverflow.com/questions/4116330/what-is-the-android-sent-sms-intent..."AFAIK, there is no Intent that is broadcast when Android sends an SMS. This is probably for privacy reasons as much as anything else." – pt2121 Mar 31 '13 at 04:18
  • Are you using your custom intent? – pt2121 Mar 31 '13 at 04:23