Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a service, where I handle the state changes(connect, disconnect, onServiceDiscoverd, onCharacteristicChange etc) and receiving data from another device through gatt Server.

My question is, Can the events be handled efficiently using Greenrobot Eventbus replacing broadcast receiver between service and Activity?

like image 513
Dinesh Ravi Avatar asked Oct 15 '15 06:10

Dinesh Ravi


People also ask

Why EventBus is used?

EventBus is an open-source library for Android and Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.

How can we transfer data from broadcast receiver to activity?

Intent intent = getIntent(); String message = intent. getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme.

How is broadcast receiver implemented in activity?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

What are the life cycle methods of broadcast receiver?

When a broadcast message arrives for the receiver, Android calls its onReceive() method and passes it the Intent object containing the message. The broadcast receiver is considered to be active only while it is executing this method. When onReceive() returns, it is inactive.


2 Answers

Unlike LocalBroadcastManager, EventBus is more simple to use. You only go via 3 steps:

1- Create an event class. A simple Java class that represent the response when the action occur.

2- Register the event bus as a subscriber in your Activity onCreate method

  EventBus.getDefault().register(this);

And of course, unregister it in your Activity onDestroy method

 EventBus.getDefault().unregister(this);

3- The subscribing method is created in the same activity that registered for the EventBus. Example in WorkOrderActivity

   @Subscribe
    public void onEvent(EventClass event)

When the event occur, you should call the post method, passing the event object you created before.

  EventBus.getDefault().post(new EventClass (Data));

As kmaini mentioned, you can replace it with LocalBroadcastManager, but you will have to map the data from the intent by yourself. Unlike EventBus which can pass objects.

Also, greenrobot, the creators of EventBus Library, answered this question here:

Q: How's EventBus different to Android's BroadcastReceiver/Intent system?

A: Unlike Android's BroadcastReceiver/Intent system, EventBus uses standard Java classes as events and offers a more convenient API. EventBus is intended for a lot more uses cases where you wouldn't want to go through the hassle of setting up Intents, preparing Intent extras, implementing broadcast receivers, and extracting Intent extras again. Also, EventBus comes with a much lower overhead.

like image 167
Abdelrahman Aly Avatar answered Oct 10 '22 05:10

Abdelrahman Aly


From another perspective, I believe broadcast managers in Android uses main thread handler's message queue to process events. So, if you are free to use a different thread (if you have no-UI events/jobs/tasks) with a proper queue (like using another HandlerThread) then you can take advantage of using that thread's specific queue to process your jobs, without interfering UI events and mixing your stuff with UI work. You can also play with the thread's priority value to balance the work.

Now, if GreenRobot provides that all functionality in a few lines of code, then I would definitely try it to see any performance gain.

like image 25
stdout Avatar answered Oct 10 '22 05:10

stdout