2016-04-25 88 views
4

我想知道是否有可能让1个侦听器在整个应用程序中侦听特定事件。设置自定义侦听器来侦听整个应用程序

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

+3

看看到奥托或greenrobot事件总线,这是最有可能为你 –

+0

感谢尖端的解决方案,将看看这个! – Max

+1

我对EventBus也有非常积极的体验,我很乐意推荐它。 – Vucko

回答

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. 
+0

Thankyou为这个答案,作品像一个魅力。 – Max