android - Is it good to replace broadcast receiver with Greenrobot Eventbus for triggering event based functions and data transfer from service to activity? -


i have implemented service, handle state changes(connect, disconnect, onservicediscoverd, oncharacteristicchange etc) , receiving data device through gatt server.

my question is, can events handled efficiently using greenrobot eventbus replacing broadcast receiver between service , activity?

unlike localbroadcastmanager, eventbus more simpler use. go via 3 steps:

1- create event class. simple java class represent response when action occur.

2- register event bus subscriber in activity oncreate method

  eventbus.getdefault().register(this); 

and of course, unregister in activity ondestroy method

 eventbus.getdefault().unregister(this); 

3- subscribing method created in same activity registered eventbus. example in workorderactivity

   @subscribe     public void onevent(eventclass event) 

when event occur, should call post method, passing event object created before.

  eventbus.getdefault().post(new eventclass (data)); 

as kmaini mentioned, can replace localbroadcastmanager, have map data intent yourself. unlike eventbus can pass objects.

also, greenrobot, creators of eventbus library, answered question here:

q: how's eventbus different android's broadcastreceiver/intent system?

a: unlike android's broadcastreceiver/intent system, eventbus uses standard java classes events , offers more convenient api. eventbus intended lot more uses cases wouldn't want go through hassle of setting intents, preparing intent extras, implementing broadcast receivers, , extracting intent extras again. also, eventbus comes lower overhead.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -