Usage pattern for dynamically registered BroadcastReceivers

When registering BroadcastReceivers dynamically in your application, you need to keep track of them and unregister them. And when unregistering you need to make sure that you haven’t done it before, else you’ll get an IllegalArgumentException. Another annoyance is when you need to check if you actually registered the receiver already or not.

The following code sample illustrates a nice way to keep track of registered receivers and only allow deleting them once.