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.
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:
Code:
#StartCommandReturnValue: android.app.Service.START_STICKY
No comments:
Post a Comment