Android 检查广播接收器是否已注册?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20329949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if the broadcstreceiver is registered?
提问by aysbtl_
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;
}
}
}
}
回答by Aman Gautam
You may want to use queryBroadcastReceiversto 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
您可能想使用queryBroadcastReceivers来查看是否有适合您意图的接收器。如果您面临多个接收器注册的问题,您可能希望看到这个
回答by ojf
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.
目前唯一的方法是调用 unregisterReceiver(receiver) 并捕获未注册时抛出的 IllegalArgumentException。拿到的时候吞下异常,你不需要对它做任何事情。
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
我已向 Google 提出功能请求以添加此 API。请在此处支持:https: //code.google.com/p/android/issues/detail?id=73718