How to make a daemon or service that can run forever and never exit in Android?

10,613

Solution 1

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.)

Solution 2

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.

Share:
10,613
Suge
Author by

Suge

Updated on June 04, 2022

Comments

  • Suge
    Suge almost 2 years

    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?