Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unregister all broadcast receivers registered in the activity

Tags:

android

Is it possible to register them all at once with a simple code? Or do they have to be unregistered one by one?

like image 638
Samet Avatar asked Feb 05 '12 12:02

Samet


2 Answers

I know it's an old question but why don't you use broadcastreceivers to pick up an intent which then triggers all receivers to unregister? (Wanted to post something more accurate than the current answer provides)

In the responding fragments/ activities you put this:

public class PanicFragment extends Fragment {

  IntentFilter killFilter = new IntentFilter("your.app.name.some.awesome.action.title");

  BroadcastReceiver kill = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
      context.unregisterReceiver(receiver); // The actual receiver you want to unreigster
      context.unregisterReceiver(this); // The one you just created

    }
  };

(Don't forget to register the receivers initially when creating the fragment/ activity)

And in your service or other activity or whatever you want this:

  private void callThisToUnregisterAllYourReceivers(Context context) {
    Intent killThemAll = new Intent();
    killThemAll.setAction("your.app.name.some.awesome.action.title");
    context.sendBroadcast(killThemAll);
  }

I hope this was in any way helpful

like image 70
AreusAstarte Avatar answered Sep 26 '22 14:09

AreusAstarte


You have to do it one by one. An activity should not have very many, if any, and so I would not expect this to be too tedious.

like image 40
CommonsWare Avatar answered Sep 25 '22 14:09

CommonsWare