Android Tutorial Creating a sticky service - long running background tasks

Discussion in 'Tutorials & Examples' started by Erel, Mar 7, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

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

    Background service example: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content

    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:
    Code:
    #StartCommandReturnValue: android.app.Service.START_STICKY
    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: Dec 9, 2018
  2. barx

    barx Well-Known Member Licensed User

    Sounds useful for non critical tasks.
     
  3. Kbogle24

    Kbogle24 Member Licensed User

    Thank you Sir this is just what I needed!
     
  4. lock255

    lock255 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?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    See the services tutorial. You should use StartServiceAt.
     
  6. lxracer

    lxracer Member Licensed User

    Regarding sticky

    #StartCommandReturnValue: android.app.Service.START_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?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    It will only start after you call StartService. It will stop when you call StopService.

    It will not be kept after a reboot. Use StartAtBoot attribute for that.
     
    IamTrying likes this.
  8. lxracer

    lxracer Member Licensed User

    Thanks
     
  9. jeronimovilar2

    jeronimovilar2 Member Licensed User

    can you send a exemple with:
    1) Service.StartForeground?
    2) #StartCommandReturnValue: android.app.Service.START_STICKY?

    thanks
     
  10. Erel

    Erel Administrator Staff Member Licensed User

  11. lxracer

    lxracer Member 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
    Code:
    Service.StartForeground(0,Null)
    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
    Code:
    #StartAtBoot: True
    and then stopservice later when widget is removed
    will service continue to start at boot?

    Thanks in advanced
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Sorry, I meant that you should start a new thread in the questions forum.
     
  13. shawny

    shawny Member Licensed User

    Anyway of starting a service without a notification icon?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  15. asales

    asales Well-Known Member Licensed User

    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.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  17. asales

    asales Well-Known Member Licensed User

  18. merlin2049er

    merlin2049er Well-Known Member Licensed User

    Will this service keep running if the screen is locked? ie. music playlist.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  20. Humberto

    Humberto Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice