所以我需要一种方法来确定是否有针对特定操作字符串注册的broadCastReceiver。
因此,为了检查意图是否可用,我们有方法(来自 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注册了特定的操作字符串。它从未将其视为已注册。但如果我发射广播。广播反应。因此,在这种情况下,方法不起作用。
想法?
答案 0 :(得分:9)
几乎相同,对于广播接收器,您应该拨打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.
查看文档,看看还能得到什么。
答案 1 :(得分:2)
try {
if (sendBroadcastReceiver != null) {
this.unregisterReceiver(sendBroadcastReceiver);
}
} catch (IllegalArgumentException e) {
sendBroadcastReceiver = null;
}