Android Question help with AUTOMATIC_FOREGROUND_ALWAYS service

giggetto71

Active Member
Licensed User
Hi Guys,
need some help. I have a Service that is started and stopped from UI (similar to B4A Bridge). Upon "Start" button pressed I do a "StartService("MyService") and upon "Stop" button I do a "StopService("MyService"). this is (simplified the Service_Create :

B4X:
Sub Service_Create
  'some code............'
    
   Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
  
   Service.AutomaticForegroundNotification = CreateNotification("xxx" ,"my service","main",False,False)
  
   Service.StartForeground(0, Notification1)
   ' start the main service task here: do something and send/cancel notifications if needed
    
End Sub

Sub Service_Destroy
   '...............'
   Service.StopAutomaticForeground
End Sub

Everything works fine and in most of cases the service once started it keeps running in background and do its job for hrs and hrs with no issues. Unfortunately I have some users complaining that in some cases the service just disappear. I think that can only mean 2 things:
1- the service crashes
OR
2- Android decides to kill it form some reasons.

For 1 I have bee trying to trace it for months looking at the Google play logs but could not find anything that helped, especially because I could never reproduce it on any of my phones (different phones, different Android versions) . For 2 I have no clue either.
Some questions:

1- I have read on one of Erel's tutorial that using AUTOMATIC_FOREGROUND_ALWAYS I should also acquire a partial wake lock. Actually I did not do that. do you think it may help?
2- what about scheduling an additional StartServiceAt service that monitors the main service and in case it finds it IsPaused = true (i.e not running) it starts it again? Would that help in case of the main service is killed by Android or in that case Android would kill the periodic monitor service too?
3 - is there a way to create a log upon service crash or in case the service is killed by the OS ?

thanks for your help..
 

Erel

Administrator
Staff member
Licensed User
Note that you can set the automatic foreground mode to 'never' as you are calling start foreground yourself. Though it should be called from Service_Start.
See this important example: Background location tracking

The partial lock prevents the CPU from sleeping. It shouldn't be needed to keep your service in the foreground.
It is possible that the users use a task manager that kills all apps including apps with foreground services. It is also possible that the OS will eventually kill the app.

- is there a way to create a log upon service crash or in case the service is killed by the OS ?
Use crashlytics.
 
Upvote 0

giggetto71

Active Member
Licensed User
following up after many many tests and at least fixed issue number 1. Now the service does not crash.
I am more and more convinced that it does not have to do with code as the service behaves just like it is intended to work on basically all phones but very few. Specifically I am fighting with a Galaxy S10 running Android 10 (other devices with Android 10 work just fine).
I have tried all possible crazy battery and other settings at OS level but still the service after some time of phone inactivity is set to sleep. As soon as I take it and log the service restarts. That applies to both my service as well as the starter service.
Is there something I can try to do to convince those phones OS to leave my service running?

The only remaining test I want to try is the idea 2 above (i.e. schedule a periodic StartServiceAt service to check if the main service is running or not.
one more question: is there any event I can catch when the service is put to sleep by the OS?

thanks you very much for any suggestion.
 
Upvote 0
Top