我在单一类中有一个广播接收器,它没有接收广播意图。
使用上下文(即getInstance(context))初始化单例。
我调用getContext()。sendBroadcast(intent);但是BroadcastReceiver没有得到任何东西。我已确认意图过滤器正在匹配。
我注册接收器的方式是我的单例构造函数,如此
private class Singleton(Context context) {
context.registerReceiver(mReceiver, INTENT_FILTER);
....
private onDestroy(Context context) {
context.unregisterReceiver(mReceiver);
....
发生了什么事?!
答案 0 :(得分:3)
好的,所以我弄清楚我做错了什么。 Singleton是由activity创建的,因此传入的上下文是 Activity Context。因此我需要将构造函数更新为:
private class Singleton(Context context) {
context.getApplicationContext().registerReceiver(mReceiver, INTENT_FILTER);
不确定为什么会有所作为。我的猜测是活动上下文被破坏了,因此BroadcastReceiver会被破坏吗?欢迎提供更多见解!