The Service
in Android will be killed if the resources of the device are not enough.How can I make a daemon or service that live forever and never exit?
2 Answers
A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

- 8,250
- 3
- 39
- 71
-
Will this be killed by third-parties thread-killer apps if I use startForeground? – Suge Aug 23 '13 at 01:55
-
Yes. You can't prevent that, your Service is running inside a normal Linux process that can be killed... – SirKnigget Aug 23 '13 at 02:12
-
I dont think third-parties thread-killer apps can kill it, coz probably then in turn do it through system only coz of android sandboxing mechanism. I am not 100% sure about it though. – Sushil Aug 23 '13 at 02:13
Using Service.startForeground(int id, Notification notification)
:http://developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification)
will tell the system that the service is needed by the user and should not be killed. The API forces you to show a notification icon during that time.
However, the system can kill anything it wants in extreme cases in order to reclaim memory.

- 3,614
- 2
- 28
- 61
-
Will this be killed by third-parties thread-killer apps if I use startForeground? – Suge Aug 23 '13 at 01:56