Android Question Long running services

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
I lack some basic understanding of services on Android.
I have an app that uploads lots of files via the SMB library. For this I use a foregroundservice. The service may run for a long time (maybe even hours) to upload all the files. Everything is done clean asynchroneously. I understand that a foreground service runs in the gui thread. So when I close the app after starting the service it finishes ok. If I leave the app open it crashes and the service stops.

So the first question is how can I run the service in a separate thread but also that it does not get killed by the os? I have found "ThreadExtras" and "Threading library". Are those what I am looking for? I never used threads in B4A. I read in this thread https://www.b4x.com/android/forum/t...-long-running-background-tasks.27065/#content that sticky services should not be used anymore. So how can I handle a long lasting task in general?

Another question is what happens to a running service if it is started again? Will it kill the already running service and start new from service_start or will it leave the old service running and just call service_start? I made a variable that I set to true if the service is running and in service_start do a return if it is set.
B4X:
if sync_running then return
sync_running = true
And in service_destroy I am setting it back to false. Is that sufficient?
 
Top