6

If you have a Service that frequently (every second or half a second) sends updates, are there pros/cons to using Broadcasts vs registering Listeners (an Interface you create) that get stored in some sort of List in the Service and sending out updates that way?

I'm thinking in terms of memory usage, battery consumption, etc. I know it's a little bit open ended, however, there's not much in terms of documentation so they can be equal, but if someone knows a definite answer or has some input, it would be appreciated.

StackOverflowed
  • 5,854
  • 9
  • 55
  • 119
  • How do you think you are going to give listener to service? – Pankaj Sep 18 '13 at 12:31
  • @Pankaj easily: get the service interface through the binder and call something like requestUpdates (like LocationManager does). – Rob Mar 26 '14 at 17:34
  • possible duplicate of [What is the difference between a Listener and a Receiver (Android)?](http://stackoverflow.com/questions/14794684/what-is-the-difference-between-a-listener-and-a-receiver-android) – Andreas Sep 10 '14 at 21:39

3 Answers3

4

In my experience, if you will send out notifications frequently, choose listeners. I've implemented some BroadcastReceivers for the same matter, but some messages got lost. I think this is because the BroadcastReceivers do not queue incoming intents but instead drop those arriving whilst still "doing work with the old one". Of course broadcasting intents can be more relaxing, as you don't have to.. connect the service and every listening part of your application, but in my case (multiple messages per second) listeners were the right choice.

damian
  • 2,001
  • 20
  • 38
0

Do not think about this things, it is really small amount of energy and perfomanse. Main difference between Broadcasts and Listeners is a way there your messages will go. If it's Broadcasts your ivents will go throgh system othervise they will go directly to your class.

Lebedevsd
  • 950
  • 5
  • 12
  • 4
    There's an old saying that says 90% of the time, the same 10% of code is executed. The listeners I have in mind are getting executed hundreds of times per hour, so in this case, even a 1% increase in efficiency would make a difference :) – StackOverflowed Oct 09 '13 at 02:13
0

Anyway you should create a BroadcastReciever to get updates (so it's still a listener). But in this case os cares to call them instead of direct listener calls from service. Not sure about value of differance, but direct calls looks faster, and less memory/battery consumpting from this pov.

Ryzh
  • 51
  • 2