Is it OK to unregister 'dynamic' BroadcastReceiver from receiver's own onReceive() method?

1.7k views Asked by At

That is, I have this BroadcastReceiver I create on the fly to listen for one broadast, after which I want it to unregister itself.

I haven't found any sample code that does it this way, but neither have I found any rule in the android online docs that forbids this. But I cannot let it hang around for as long as the activity, and it is in an anonymous class anyway, so the containing class does not even know the variable name.

That is, the code looks something like this:

myInfoReceiver = new BroadcastReceiver() {
onReceive(Context ctx, Intent intt) {
    // do some Notification when I get here
    nm.notify("I got here") // obvious pseudo code
    ctx.unregisterReceiver(myInfoReceiver);
} // end onReceive
ctx.registerReceiver),uInfoReceiver, new IntentFilter(...));
}; // end BroadcastReceiver

But when I run this, Android complains when it calls unregister, insisting that the receiver is not there to unregister (I forget the exact wording, but it threw IllegalArgumentException).

I also tried modifying the code to check that the action in 'intt' is the same as expected -- but then it still executest onReceive but silently fails to unregister.

3

There are 3 answers

6
David Wasser On BEST ANSWER

The answer to your question is "yes". However...

...you need to call unregisterReceiver() on the same Context that you called registerReceiver() on. In the code you posted you are calling unregisterReceiver() on the Context passed in as an argument to onReceive(). This is not the same Context which is why you are getting the exception.

4
shkschneider On

I would simply say YES, no problem at all.

I use it for a one-time location fix for example, and can be used in other logics too without me seeing any problem to it.

Plus I've seen it around many times.

0
Dominique Lorre On

I have tried various solutions, finally I am doing it that way:

Registering:

MyApplication.getInstance().getApplicationContext().registerReceiver(sentReceiver, new IntentFilter(SENT));

SentReceiver:

public class SentReceiver extends BroadcastReceiver  {
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context,
                        context.getString(R.string.sms_envoye), Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(context,
                        context.getString(R.string.sms_defaillance_generique),
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(context,
                        context.getString(R.string.sms_pas_de_service),
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(context,
                        context.getString(R.string.sms_pas_de_pdu),
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(context,
                        context.getString(R.string.sms_radio_desactivee),
                        Toast.LENGTH_SHORT).show();
                break;
        }
        MyApplication.getInstance().getApplicationContext().unregisterReceiver(this);
    }

With MyApplication:

public class MyApplication extends Application {
    private static MyApplication mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }        
}