Android Question Sticky Service not working on Android 8.0

Discussion in 'Android Questions' started by Inman, Aug 1, 2018.

  1. Inman

    Inman Well-Known Member Licensed User

    I have a Sticky Service that needs to be up all the time but doesn't restart itself when target SDK is 26. I did read the thread on Android 8.0 but still have some doubt regarding this.

    Firstly, here is what my app does:
    • It runs a service that has a FileObserver monitoring a particular folder
    • When a new file is created in this folder, FileObserver event is raised, upon which I do some modifications to the new file
    • Then the file is moved to a different folder
    To keep the above-mentioned service alive all the time, it is both sticky and set to start at boot. This has been working fine until I started targetting SDK 26. Now when the service gets killed, the system does not restart it anymore.

    How can I make sure the system restarts the service when it is killed, just like how it used to behave when target SDK was below 26? Also is it possible to turn off via code, the persistent notification that is usually up when a service is running?
  2. Erel

    Erel Administrator Staff Member Licensed User

    Sticky services should no longer be used. They will not work properly and will be killed quickly.

    You should use a foreground service if you want to keep the process running in the background.

    Not without exiting foreground mode.
  3. Inman

    Inman Well-Known Member Licensed User

    What change should I make to the present code to make the service self-restart? By present code I mean the service has just the following attributes:
    • #StartAtBoot: True
    • #StartCommandReturnValue:
  4. Erel

    Erel Administrator Staff Member Licensed User

    You should remove the second line and make your service a foreground service.

    You can do it by adding this line to Service_Create:
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    Search for Service.StartForeground for other options.
  5. Inman

    Inman Well-Known Member Licensed User

    Ok great. Thank you for clarifying.

    Another doubt. In your other thread, you mentioned this:
    This could work in the case of the push notifications example you mention where the service can stop itself as soon as it receives the push notification and can wake up when the next push arrives.

    But in my case the service needs to be alive all the time as FileObserver won't work otherwise. Does that mean the service will always be a foreground service and that it will always have the persistent notification?

    Also how does foreground service impact battery life when compared to sticky services in Android 8.0? Does it hold a wakelock thereby not allowing the device to go to sleep? I am asking this because whenever there is a persistent notification, users get alarmed and some even leave negative reviews on Play Store saying the app is draining battery even though the reality might be different.
  6. Erel

    Erel Administrator Staff Member Licensed User


    No. However the process will keep running so it will have some impact.

    You can instead use StartServiceAt to start your app every hour do whatever it needs to do and stop.
    Inman likes this.
  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