Unregister all broadcast receivers registered in the activity

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

+4
source share
3 answers

You must do this one by one. Activity should not have very much, if any, and therefore I would not expect this to be too tiring.

+5
source

I know this is an old question, but why don't you use the broadcast to get the intent, which then starts all receivers to register? (Requires to post something more accurate than the current answer gives)

In the response snippets / actions 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 } }; 

(Remember to register receivers when creating a fragment / activity)

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

  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 useful anyway.

+5
source

I would not use another BroadcastReceiver to remove other broadcast receivers.

Here is what I added to my application class:

 private static List<BroadcastReceiver> broadcastReceivers = new LinkedList<>(); public void addReceiver(BroadcastReceiver receiver, IntentFilter filter) { mContext.registerReceiver(receiver, filter); broadcastReceivers.add(receiver); } public void removeReceiver(BroadcastReceiver receiver) { unregisterReceiver(receiver); broadcastReceivers.remove(receiver); } public List<BroadcastReceiver> getAllReceivers() { return broadcastReceivers; } public void removeAllReceivers() { for (BroadcastReceiver receiver : getAllReceivers()) { removeReceiver(receiver); } } 
0
source

Source: https://habr.com/ru/post/1394859/


All Articles