How to check if Receiver is registered in Android?
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 aBroadcastReceiver
implementation that calls
Context#unregisterReceiver(BroadcastReceiver)
passing itself as the argument after handling the Intent that it receives.
There is a small chance that the receiver'sonReceive(Context, Intent)
method is called
more than once, since it is registered with multipleIntentFilters
, creating the potential for anIllegalArgumentException
being thrown fromContext#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.
Check if the broadcstreceiver is registered?
You may want to use queryBroadcastReceivers to see if there is a receiver for your intent or not. If you are facing problem of multiple receiver registration, you may want to see this
how to check broadcast unregister or not in android?
Add following method in your activity and call it in your onResume and in your onActivityResult callbacks. Once method is called it will set your myBroadcastReceiver instance to null, so it will avoid to execute multiple times until you create a new myBroadcastReceiver instance.
private void unregisterMyBroadcastReceiver() {
if (null != myBroadcastReceiver) {
unregisterReceiver(myBroadcastReceiver);
myBroadcastReceiver = null;
}
}
How to check the broadcast receiver is running or not in android?
If you want to check it at runtime you can store a global boolean
variable and set it to false
and inside your onReceive()
set it to true
and before the onReceive()
exit set it back to false .
any time you can check this global variable to tell if that broadcast receiver is running or not .
if you mean you want to know if its work or not make a message to the log cat example
onReceive(){
Log.d("my broadcast","works");
}
What's wrong with my register broadcast receiver?
This code:
Intent intent = context.registerReceiver(receiver, intentFilter);
will return null
because it only returns "sticky" broadcasts. This is correct, as you aren't sending a "sticky" broadcast.
This code:
List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);
should return an empty list because this code only returns BroadcastReceiver
s that have been registered in the manifest with <receiver>
tags, and does NOT return anything for dynamically registered BroadcastReceiver
s, which your MyBroadcastReceiver
is.
I do not know why onReceive()
was not called. Please post your MyBroadcastReceiver
code. I don't see anything obvious. You could try this adb command instead:
am broadcast -a DUMMY
You could also try adding
sendBroadcast(queryIntent);
after this code:
Intent queryIntent = new Intent(MY_ACTION);
and see if this calls your onReceive()
.
Related Topics
Edittext Seterror() with Icon But Without Popup Message
Hide Ics Back Home Task Switcher Buttons
Android: Unable to Instantiate Activity/Classnotfoundexception
Drawable-Hdpi, Drawable-Mdpi, Drawable-Ldpi Android
How to Implement a Contentobserver for Call Logs
Adding Local .Aar Files to My Gradle Build
Android: Overlay on Android Camera Preview
Android: Tabhost Without Tabactivity
Android Studio - Creating Modules Without Copying Files
How to Get Child of Child Value from Firebase in Android
Google Glass Gdk: How to Communicate with Android Device
How to Update My Adt in Eclipse
How to Detect When the User Has Changed the Clock Time on Their Device