tags:

views:

260

answers:

2

I need to check if my registered receiver is still registered if not how do i check it any methods?

+1  A: 

I am not sure the API provides directly an API, if you consider this thread:

I was wondering the same thing.
In my case I have a BroadcastReceiver implementation that calls Context#unregisterReceiver(BroadcastReceiver) passing itself as the arg after handling the Intent that it receives.
There is a small chance that the receiver's onReceive(Context, Intent) method is called more than once, since it is registered with multiple IntentFilters, creating the potential for an IllegalArgumentException being thrown from Context#unregisterReceiver(BroadcastReceiver).

In my case I can store a private synchronized member to check before calling Context#unregisterReceiver(BroadcastReceiver), but it would be much cleaner if the API provided a check method.

VonC
+2  A: 

There is no API function to check if a receiver is registered. The workaround is to put your code in a try {...} catch(IllegalArgumentException e) {...} block.

Daniel Velkov