Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if there is a BroadCastReceiver registered with action string

So I need a way to find out if there is a broadCastReceiver registered for a specific action string.

So to check if intent is available we have method (from http://www.vogella.com/articles/AndroidIntent/article.html)

public boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> resolveInfo =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
   if (resolveInfo.size() > 0) {
        return true;
    }
   return false;
}

Which works but from my tests looks only for intents that activities are registered to. I have a broadCastReceiver registered for a specific action string. and it never sees it as registered. But if I fire broadcast. broadcast reacts. So method don't work in this case.

Ideas?

like image 419
pulancheck1988 Avatar asked Apr 24 '12 07:04

pulancheck1988


2 Answers

try {
    if (sendBroadcastReceiver != null) {
        this.unregisterReceiver(sendBroadcastReceiver);
    }
} catch (IllegalArgumentException e) {
    sendBroadcastReceiver = null;
}
like image 38
Moien.Dev Avatar answered Nov 06 '22 03:11

Moien.Dev


It's almost the same, for broadcast receivers you should call PackageManager#queryBroadcastReceivers.

public abstract List<ResolveInfo> queryBroadcastReceivers (Intent intent, int flags)

Since: API Level 1
Retrieve all receivers that can handle a broadcast of the given intent.

Parameters
intent  The desired intent as per resolveActivity().
flags   Additional option flags.

Returns
A List<ResolveInfo> containing one entry for each matching Receiver. These are ordered from first to last in priority. If there are no matching receivers, an empty list is returned.

Look at the docs, to see what else can you get.

like image 103
MByD Avatar answered Nov 06 '22 03:11

MByD