Registering sbscribers
With the Publisher
/Sender
class and event class already specified, all that remains is to register our Activity
class to receive both events and print the event sent on the screen.
Like we stated before, to receive any event from the Bus
, the Subscriber
entity, which could be any Java class on your code, will have to register on the Bus and subscribe to the event that it is interested in.
Any object will have to register on the Bus by calling the register function and provide a single on<EventName>(EventType)
method annotated with org.greenrobot.eventbus.Subscribe
for all the kind of event that it is interested in:
@Subscribe void on<EventClassname>(EventClassname event) { ... }
Let's implement the functions that are going to handle the MobileNetDisconnectedEvent
and the MobileNetConnectedEvent
event in our Activity:
@Subscribe public void onMobileNetDisconnectedEvent(MobileNetDisconnectedEvent event){ String message = String.format( "Mobile connection...