I have implemented a background Service that is running in the same process as my app. My problem is that when the app is closed (tab on windows button, then swipe app to the left to close it), it causes the whole process to close and the Service to stop working as well.
On the OnStartCommand() callback in the background service, I return START_STICKY.
It takes about 5 seconds for the process to appear as closed (0 processes and 1 service) on app settings page as below
Currently to overcome this problem, in my activity OnDestroy() callback I set an alarm-manager to start my service again after 10 seconds -giving enough time for process to fully close before opening it again. This solution works to an extent, but it's not what I want. What I really want is for my Service not to close from the first place.
I have looked at the option of making the Service in a separate process, however, I then found major difficulties in using variables and functions on the service class and stuff since they are now in separate memory stacks.
I know about the foreground service solution but unfortunately having the that notification stick there all the time is very intimidating.
Is there any good solution to keep the service running even when the user closes the app ?