Android Question Sticky Service not working on Android 8.0

Inman

Well-Known Member
Licensed User
Longtime 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?
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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.

Also is it possible to turn off via code, the persistent notification that is usually up when a service is running?
Not without exiting foreground mode.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime 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: android.app.Service.START_STICKY
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Ok great. Thank you for clarifying.

Another doubt. In your other thread, you mentioned this:
You need to do one thing which is to stop the automatic foreground mode when the service completed its task.
This can be done in two ways:

1. Call Service.StopAutomaticForeground. This will stop the automatic foreground mode if the service was in that mode. Otherwise it will not do anything.
It is safe to call it multiple times. The service will not be immediately destroyed.

2. Call StopService(Me).

If you don't stop the automatic foreground mode then a notification icon will be left after the task completed.
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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Does that mean the service will always be a foreground service and that it will always have the persistent notification?
Yes.

Does it hold a wakelock thereby not allowing the device to go to sleep?
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.
 
Upvote 0
Top