Android Tutorial Creating a sticky service - long running background tasks

Edit: Things have changed with newer versions of Android and new restrictions. Sticky services should not be used any more.

Background service example:

This tutorial is not 100% correct anymore.

There are several ways to handle background tasks.
1. You can use StartServiceAt to schedule a service to run at a specific time. When the service is started you can schedule the next one.

This way you can create a service that runs every x minutes or hours. This option is good for all kinds of updates. The service can connect to your server and check whether there are any new messages for example. (Note that for "realtime" notifications you should use the Push framework)

2. If you have a long running task and you don't want the OS to kill your process when there is no visible activity then you should call Service.StartForeground. Android will treat your application as if it is a visible application and will not kill it.

However an ongoing notification icon will be added to notify the user that your application is actively running. A music player for example should use this option.

3. The third option which is supported through the attributes modules feature is to mark your service as a sticky service.

This is done by adding the following attribute to the service code:

A sticky service is somewhere between a regular service and a foreground service. Android will kill the process from time to time. However it will recreate it automatically (as soon as possible). For example if you want to track the device location you can use this option. The service (together with the process) will be killed however it will also be recreated again.

Related tutorial: The result of swiping an app from the recent apps list
Last edited:


Well-Known Member
Licensed User
Sounds useful for non critical tasks.


Well-Known Member
Licensed User
Erel I have the need to activate a service every 30 minutes just the time to check a link and then the service you have to kill, my posts please the sample code to do this?


Licensed User
Regarding sticky


what is the life cycle of it
will it not start untill Startservice() is called
and will it stay off if i call stopservice()?
and if i never stopservice will it remain alive even with reboot?


Licensed User
can you send a exemple with:
1) Service.StartForeground?
2) #StartCommandReturnValue:



Licensed User
Hello Erel thanks again,
i wasnt sure if when you said post new thread for that was
referring to his question for example

i have a question tho

i did this
seems to work fine no error came out,
is there a better way to do it?
it is a widget that doesnt need notification

also one more question, if i use
#StartAtBoot: True
and then stopservice later when widget is removed
will service continue to start at boot?

Thanks in advanced


Well-Known Member
Licensed User
... The service can connect to your server and check whether there are any new messages for example ...
How I can do this? What I need to do to my app check if exists a new message in a server, every week, and show a notification? Thanks.