Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much does broadcast receiver cost for memory?

Scope: Have to update activity UI in different ways. Update depends on broadcasts received from service. Problem: There are two common ways to find out which way UI should be updated:

  1. register only 1 broadcast receiver but put different extras in its intent and check for them in OnReceive() method in activity;
  2. register broadcasts for each update command.

The 2nd ways seems to be more elegant and more understandable. But I wonder if it will consume more memory. What would you recommend? Thanks!

like image 423
Oleksii Malovanyi Avatar asked Oct 24 '11 15:10

Oleksii Malovanyi


People also ask

What is a broadcast receiver?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

When would you use a broadcast receiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What is the time limit of broadcast receiver in Android?

There is also a 5-10 second limit, after which Android will basically crash your app. However, you cannot reliably fork a background thread from onReceive() , as once onReceive() returns, your process might be terminated, if you are not in the foreground.

How do you inject a broadcast receiver?

inject(this, context); in onReceive method. Here is the receiver so you don't have to do that, the trick is extending from DaggerBroadcastReceiver : class MyReceiver : DaggerBroadcastReceiver() { @Inject lateinit var repository: MyRepository override fun onReceive(context: Context?, intent: Intent?) { super.


2 Answers

It should not make a major difference either way.

like image 166
CommonsWare Avatar answered Oct 19 '22 23:10

CommonsWare


I agree with CommonsWare. From a perfomance standpoing this isn't really something you need to worry about. That said, I'd probably go with 2nd way for the sole reason that it will make your code more modular thus improving maintainability.

like image 40
Kurtis Nusbaum Avatar answered Oct 19 '22 22:10

Kurtis Nusbaum