设置自定义侦听器以在整个应用程序中进行侦听

时间:2016-04-25 21:57:52

标签: android listener android-lifecycle

我想知道是否有可能让1名听众在整个应用程序中监听特定事件。

此时我已经创建了自己的侦听器,正在侦听事件发生,但是当我切换到另一个活动时它仍应该听这个事件。

1 个答案:

答案 0 :(得分:3)

使用http://square.github.io/otto/进行事件监听。

这是一个使用示例:

Bus bus = new Bus();

bus.post(new AnswerAvailableEvent(42));

@Subscribe public void answerAvailable(AnswerAvailableEvent event) {
    // TODO: React to the event somehow!
}

bus.register(this); // In order to receive events, a class instance needs to register with the bus.