如何检查 Receiver 是否已在 Android 中注册? [英] How to check if Receiver is registered in Android?

查看:37
本文介绍了如何检查 Receiver 是否已在 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 a BroadcastReceiver implementation that calls Context#unregisterReceiver(BroadcastReceiver) passing itself as the argument after handling the Intent that it receives.
There is a small chance that the receiver's onReceive(Context, Intent) method is called more than once, since it is registered with multiple IntentFilters, creating the potential for an IllegalArgumentException being thrown from Context#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆