Android Question service lyfe cycle when system kills a service

coslad

Well-Known Member
Licensed User
Longtime User
HI

someone says that the way to keep a service active is to monitor it through another "watch dog" service, but I do not think it is the right way, because it is not certain that the "watch dog" service has also not been closed by the OS .

What appen to my service when the os kills it ?

Is it called the destroy subs ?

if yes , is it possible to restart the service from destroy subs ? maybe starting another service which after one second restarts my service ?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The exact behavior depends on the OS version.

Up to Android 8 the OS will never kill a service. It will kill the complete process. No event will be raised when that happens.
Starting from Android 8 the OS can either kill the complete process or kill active services (when the app is in the background). Service_Destroy is called when the OS kills a service.
You cannot start services while your app is in the background with the exception of foreground services (B4A will do it automatically for you in most cases).

All of this is not too important.

If you want your service to keep running then you should make it a foreground service.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…