Can a service be made to run continuously? (Service restarts when app is fully closed.)

225 views
Skip to first unread message

davefinney

unread,
Jul 11, 2022, 10:27:38 AM7/11/22
to DroidScript
I have an app with a service.
When the app is closed (Back button), the service continues as intended.

But when the app is removed from the list of recent apps (fully closed), the service restarts.

Can this be prevented such that the service simply runs continously?

The problem is the service loses it's variables.
A work around might be for the service to save it's variables as user settings before the app closes, and then load them back in when starting, but this is not ideal.

Dave

unread,
Jul 12, 2022, 5:05:45 AM7/12/22
to DroidScript
Unfortunately you can never tell when Android will decide to kill your service, so it's best to save and reload service variables using the app.SaveNumber, app.SaveText, app.LoadNumber etc methods every time they are changed to maintain state.

On some phones swiping away an app kills the both app and its running services too, but not on all devices (in my experience).

Also, Android may just shut down your service when memory or battery gets low and your main app is not in the foreground.  You can reduce the chance of this by making sure your service is a 'foreground service' and shows a continuous notification.

It is often better to use a 'Background Job' instead of a service if possible as that is normally more durable than a service.  These can be set to run roughly every 15 minutes to do tasks like checking a server for new data/messages etc (See the 'Background Job' app template)

davefinney

unread,
Jul 12, 2022, 7:09:52 AM7/12/22
to DroidScript
Thanks Dave,
Will try saving settings each change.
Dave.

Reply all
Reply to author
Forward
0 new messages