在做Android项目时,有时候需要知道某个BroadcastReceiver是否已经注册,从而决定是发送广播让Receiver处理,还是通过其他方式处理。
那么,如何判断某个广播接收者BroadcReceiver是否已经注册了呢?
PackageManager 类中提供了 List<ResolveInfo> queryBroadcastReceivers(Intent
intent, int flags) 方法来查询Intent对应的所有广播接收者。
使用举例:
Intent intent = new Intent();
intent.setAction("com.xxx.powersaving.INSTALLAPP");
intent.putExtra("path", apkPath); PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(intent, 0);
if(resolveInfos != null && !resolveInfos.isEmpty()){
//查询到相应的BroadcastReceiver
}