如何检查 Receiver 是否已在 Android 中注册? [英] How to check if Receiver is registered in Android?
问题描述
我需要检查我注册的接收器是否仍然注册,如果没有我如何检查它任何方法?
I need to check if my registered receiver is still registered if not how do i check it any methods?
推荐答案
我不确定 API 是否直接提供 API,如果您考虑 这个线程:
I am not sure the API provides directly an API, if you consider this thread:
我也在想同样的事情.
就我而言,我有一个 BroadcastReceiver
实现,它调用Context#unregisterReceiver(BroadcastReceiver)
在处理接收到的 Intent 后,将自身作为参数传递.
接收者的 onReceive(Context, Intent)
方法被调用的可能性很小不止一次,因为它注册了多个 IntentFilters
,这就产生了从 Context#unregisterReceiver(BroadcastReceiver)
抛出 IllegalArgumentException
的可能性.
I was wondering the same thing.
In my case I have aBroadcastReceiver
implementation that callsContext#unregisterReceiver(BroadcastReceiver)
passing itself as the argument after handling the Intent that it receives.
There is a small chance that the receiver'sonReceive(Context, Intent)
method is called more than once, since it is registered with multipleIntentFilters
, creating the potential for anIllegalArgumentException
being thrown fromContext#unregisterReceiver(BroadcastReceiver)
.
在我的例子中,我可以在调用 Context#unregisterReceiver(BroadcastReceiver)
之前存储一个私有的同步成员来检查,但它会是如果 API 提供了检查方法,则更清晰.
In my case, I can store a private synchronized member to check before calling Context#unregisterReceiver(BroadcastReceiver)
, but it would be
much cleaner if the API provided a check method.
这篇关于如何检查 Receiver 是否已在 Android 中注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!