How to keep service alive?

1.7k Views Asked by At

Any application that has a service going obviousely counts on it to be always running. It's part of the product and if it's a paid app - the user expects it.

However, it seems that there is no way for a service not to get wacked if the Android OS feels the memory pressure or if something goes wrong and the service crashes.

What is the best way of making sure that a service is always running?

Is it wise to create a second service that once in a while start the "useful" service and if it's alredy running it'll be ignored?

2

There are 2 best solutions below

0
On

To be honest, I am not sure if this is a good idea as all.

If there is enough memory pressure, it would not be fair towards other apps to insist on running. Also services only get kicked out on severe low memory situations.

If you write a reaper service, this one can get kicked out too.

And as you say " and if it's a paid app - the user expects it." -- the user also expects that apps play nicely

1
On

If your service is 'critical', you can use startForeground which gives your service as much priority as an Activity in the foreground.