How can we prevent a Service from being killed by OS?

You may run the service in the foreground using startForeground().

A foreground service is a service that’s considered to be something
the user is actively aware of and thus not a candidate for the system
to kill when low on memory.

But bear in mind that a foreground service must provide a notification for the status bar (read here), and that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Note: This still does not absolutely guarantee that the service won’t be killed under extremely low memory conditions. It only makes it less likely to be killed.

Leave a Comment