Otto is an event bus, forked from Guave’s event bus. It is specialized for android platform. It is lightweight, easy to use, and greatly simplifies the communication between your application components. Using eventbus instead of Broadcast Intents, PendingIntent or a Messenger - is a great solution for your application.

I have prepared a sample demo of Otto,which is available in https://github.com/shrek2000/OttoPlayground The demo shows how Otto can serve as a platform of communication between different fragments, and a service.

The following are interesting issues I encountered during my work

Service - UI communication

The event bus is created as singleton. So all subscribers and publisher can access the same instance from the BusProvider class.The UI thread has its own designated event bus, which is crerated with ThreadEnforcer Main. This event bus can only be accessed for main UI thread. The other event bus use ThreadEnforcer of Any, since the service need to consume its events from its own thread. But what is we want to send an event from service thread to UI thread? In order to send an event from service thread to UI thread I used a handler to execute the publish function on the ui thread: Handler handler = new Handler(Looper.getMainLooper()); …

handler.post(new Runnable() {

       @Override


       public void run() {


           uiBus.post(new LabelMessage(i, messages[j]));


       }


   });

Polymorphism

Otto support for polymorphism is demonstrated in function commandLogger. There are two types of Commands, startCommand and stopCommand. Function commandLogger is subscribed to the base class. Therefore, it receives both commands types.