7

I have two receivers that are listening for android.intent.action.BOOT_COMPLETED and android.intent.action.PACKAGE_REPLACED. I was wondering how much battery life they are causing my phone to consume since they cause my app to constantly run now.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
ninjasense
  • 13,756
  • 19
  • 75
  • 92
  • 2
    If you don't need to know about *every* package changing, then you can narrow the scope of your intent filter to only catch the package you're interested in, thereby only starting your app process rarely. – Christopher Orr Dec 28 '10 at 21:45
  • If I only catch my package, will the app still appear in app killers all the time or only when it is recieveing a broadcast? – ninjasense Dec 28 '10 at 21:48
  • Task killers are generally a poor measure of whether something is actually running. Android caches processes; task killers tend to report these processes and blame their existence on the last application code that ran in them. You can see similar behavior in DDMS. – CommonsWare Dec 28 '10 at 21:53
  • @CommonsWare I agree, however, the average user running the app I am creating does now realize this. Even if I try to educate them, unfortunately most wont listen. Kind of a losing situation as most people don't realize that apps are not meant to be killed like that. – ninjasense Dec 28 '10 at 22:08

1 Answers1

13

The broadcast receivers themselves will not directly consume much battery life. BOOT_COMPLETED happens once; PACKAGE_REPLACED happens only on an application upgrade. Those probably average one event per day.

Now, if those broadcast receivers do other stuff, such as starting services, that may have significant battery implications...but that is a problem with your services, not with the receivers themselves.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What if I declare various registered broadcast receivers across the app and each of them listens to whatever is needed where they are registered? For example, multiple receivers might listen to volume changes, battery changes... Might that consume much power? I'm imagining a while loop waiting for a broadcast, and for each receiver, I imagine one loop, so multiple receivers, multiple loops. No idea if that's how it works though. – Edw590 Jul 26 '21 at 01:54
  • 1
    @DADi590: "Might that consume much power?" -- if that concerns you, do a more efficient job in your app, by avoiding "multiple receivers might listen to volume changes, battery changes". For example, you could have a `SystemRepository` that is the one spot for listening for system events, where `SystemRepository` republishes those events via an in-app reactive API (e.g., coroutines `SharedFlow`, RxJava `PublishSubject`, `LiveData`). That would be incrementally more efficient than would be registering for the same broadcast N times. Neither should have a major power impact, though. – CommonsWare Jul 26 '21 at 11:17