11

Is there any way to check if the broadcast receiver is working or not.

I did it like below. I register the broadcast receiver but it doesn't find this registered receiver..

PackageManager pm = getApplicationContext().getPackageManager();
final List<PackageInfo> packs = pm.getInstalledPackages(PackageManager.GET_RECEIVERS);
    for (final PackageInfo p : packs) {
        ActivityInfo[] receivers = p.receivers;
        if (receivers != null) {
            for (ActivityInfo ai : receivers) {
                if(AppDetectionService.class.getName().equals(ai.name)){
                    onOff[2] = true;
                }
            }
        }                   
    }
Kara
  • 6,115
  • 16
  • 50
  • 57
aysbtl_
  • 339
  • 1
  • 3
  • 18

2 Answers2

11

Currently the only way to do this is to call unregisterReceiver(receiver) and catch the IllegalArgumentException that's thrown if it's not registered. Swallow the exception when you get it, you don't need to do anything with it.

I've raised a feature request to Google to get this API added. Please support it here: https://code.google.com/p/android/issues/detail?id=73718

ojf
  • 163
  • 1
  • 6
9

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

Community
  • 1
  • 1
Aman Gautam
  • 3,549
  • 2
  • 21
  • 25
  • 1
    do you know how to use this queryBroadcstReceiver(intent,flag). Actually i couldn't get. what is intent and flag. I just want to get all registered receivers. and check if a specific receiver is registered. – aysbtl_ Dec 05 '13 at 14:45