2016-04-25 150 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