0

I need my app to start a service. I have read many questions including this one (see Answer 1): Trying to start a service on boot on Android

I am pretty happy with Answer 1 but have a question regarding the RECEIVE_BOOT_COMPLETED. Will this only be triggered after the boot, or will it also be triggered (or emulated) when the user installs or first-time starts the app, or do I have to make sure for myself that the service is started the first time via another method, as the device did not technically boot up and technically should not send the BOOT_COMPLETED message.

Nidhoegger
  • 4,973
  • 4
  • 36
  • 81
  • https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED – AskNilesh Feb 11 '19 at 07:39
  • Thanks Nilesh, I already read this, but it does not clarify my question if I need a fallback mechanism for manually starting it if the device did not boot. – Nidhoegger Feb 11 '19 at 07:44

1 Answers1

1

RECEIVE_BOOT_COMPLETED is a broadcast message that can only by sent by the System. Additionally, as seen from this documentation, it is a Broadcast that's only sent once.

Therefore, your Broadcast Receiver for RECEIVE_BOOT_COMPLETED will not trigger upon the first install or the first run of your app. Your Android Device will only broadcast this message once, and that's when it completes it's first boot.

So if you wish to ensure your Service is running, it's not enough to rely on the Boot Complete Broadcast. After all, if the system decides to kill your Service for whatever reason, such as needing memory, then your Service is dead until the next boot... which isn't what you want.

Honestly, the RECEIVE_BOOT_COMPLETED broadcast is mainly used as a supplementary method for starting a Service rather than it's primary method.

Jackey
  • 3,184
  • 1
  • 11
  • 12
  • Thank you very much for this detailed answer. I will do it as a supplementary method and use something else as primary method – Nidhoegger Feb 11 '19 at 08:18
  • If you can avoid listening for boot entirely, then avoid it. The reason why boot is so slow on android is because every app wants to be started on boot. – Eugen Pechanec Feb 11 '19 at 08:22