I am testing this on Samsung Galaxy Note 8 running Android version 8.0.0
These are my observations.
In the service_create I have used Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
End Sub
Method 1: Remove the code Service.StopAutomaticForeground from the Service_Start
This test is running fine for more than 2 hrs and the service is running every 15 minutes, so far good. This method is working fine, but always displays a non-dismiss-able notification with the app's name on the device.
I don't prefer to have this notification displayed ALWAYS on the device. Would like to know that is there a way to get this working without having non-dismiss-able notification always ?
Method 2: Include the code Service.StopAutomaticForeground in the Service_Start
Applying this method, it works up to 45 minutes, after that the service stops. I assume that the OS kills this eventually. Good thing is that the non-dismiss-able notification is NOT there on the device.
If I keep the code Service.StopAutomaticForeground in the Service_Start, the Service works for 3 times (scheduled to run every 15 minutes), after that even though the StartServiceAt(Me, nAfter15Minutes, False ) is called/scheduled, the service dies itself and is never executed.
As per the notes, it is advised to keep the 3rd parameter as False in StartServiceAt() or else there may be heavy battery drainage, so I have used False.
So my question is that
- Whether a scheduled service can work WITHOUT always displaying a non-dismiss-able notification on the device ?
- Is Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED is the solution to avoid the ALWAYS non-dismiss-able notification on the device ? In that case what will happen if the OS kills the app ? Will the scheduled service work ?
- Will this works the same way on all Android version below 8.0.0
- As of now, I have used Pw.PartialLock in Service_Start is this really required ?
Any help will be appreciated.
Update : Tried using Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED instead of Service.AUTOMATIC_FOREGROUND_ALWAYS. The result is that the schedule will not work after 45 minutes. So this is not a solution