How to Check If Receiver Is Registered in Android

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 a BroadcastReceiver 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'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.

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 BroadcastReceivers that have been registered in the manifest with <receiver> tags, and does NOT return anything for dynamically registered BroadcastReceivers, 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



Leave a reply



Submit