Do I need to unregister an “anonymous” BroadcastReceiver

I recently asked a question about checking the status of a sent SMS, and the answer was a piece of code that registered two “anonymous internal” ones (please correct my terminology if this is incorrect) BroadcastReceiversin order to listen to the sent SMS / delivered broadcasts. These receivers were only needed to receive data that my application was just sent, so I did not need to listen constantly.

My immediate thought was “OK, I will need to unregister after I’ve finished with them,” but is that right? I asked for a poster because it did not include any unregistered code, but did not receive a response. The code seems like a pretty standard way of doing what I want, as it appears on numerous Android dev sites. There he is:

//---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);

The code is working fine.

What's more, he does not receive notifications about any sent or sent SMS messages that occur outside of my application. For instance. I can send SMS after these BroadcastReceivershave been registered and do not see messages Toast.

So, I have two questions:

  • Do I need to unregister these BroadcastReceivers?
  • If not, why not?
+5
4

BroadcastReceiver , , -)

:

registerReceiver(new BroadcastReceiver(){

BroadcastReceiver smsReceiver=new BroadcastReceiver(){...}
registerReceiver(smsReceiver);

, :

unregisterReceiver(smsReceiver);

smsReceiver .

+1

, - SMS-, .

. , . , . .

+1

. , , . . , , , , :)

Android , . , . . Activity.onResume(), Activity.onPause(). ( , ). unregister Activity.onSaveInstanceState(), , . " < =====

0

, , , , BroadcastReceiver sendSMS(), , SENT DELIVERED Intents, , . , , , , , .

, , sent_broadcast_receiver delivered_broadcast_receiver, , , , .

EDIT: No matter which object BroadcastReceivers holds for SENTand DELIVERED, it should persist until intentions return, or intentions (as they are PendingIntents) should direct to what can be woken up if that is what you want .

0
source

All Articles