Search code examples
androidandroid-servicesamsung-mobile

Android service is stopped when going to Home screen on some devices


As the title suggests, I've got a service that adds an icon on top of all other screens...kind of what Facebook Head or Link Bubble does. That works fine on most devices, the icon is always there.

On a Samsung S3, however, whenever I press the device's Home icon, the icon disappears and I think the service stops. Why is that?


Solution

  • Looks like the service is stopped when the main app that stared it is closed...which can happen at different times on different devices. More info here: Android Service Stops When App Is Closed

    More info:

    • when the activity that started the service is closed (by the user, or by the system), the service is stopped as well. However, it starts automatically again as soon as possible (how fast depends on the device)
    • my service was already a background service...and in order to be kept always running, it should have been marker as foreground instead (with startForeground). Problem is that a sticky notification needs to be always present in the notification area, which would be very annoying.

    In the end, I think the current behaviour works fine for most current devices that don't automatically close an app when the activity is put in background...and that anyway are able to restart the service automatically shortly after stopping the service because of that. If that wasn't enough, then I'd had to use startForeground.