6

所以我需要一种方法来确定是否有一个为特定操作字符串注册的广播接收器。

因此,要检查意图是否可用,我们有方法(来自 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;
}

哪个有效,但从我的测试中只查找活动注册到的意图。我为特定的操作字符串注册了一个 broadCastReceiver。并且它永远不会将其视为已注册。但如果我发射广播。广播做出反应。所以方法在这种情况下不起作用。

想法?

4

2 回答 2

10

几乎相同,对于广播接收器,您应该调用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.

查看文档,看看你还能得到什么。

于 2012-04-24T07:43:16.380 回答
1
try {
    if (sendBroadcastReceiver != null) {
        this.unregisterReceiver(sendBroadcastReceiver);
    }
} catch (IllegalArgumentException e) {
    sendBroadcastReceiver = null;
}
于 2014-08-19T05:27:51.083 回答