Nickelgrass Active Member Licensed User Longtime User Oct 2, 2023 #1 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?
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?
Erel B4X founder Staff member Licensed User Longtime User Oct 3, 2023 #2 It has nothing to do with threads. Nickelgrass said: If I leave the app open it crashes and the service stops. Click to expand... Post the logs. Upvote 0
It has nothing to do with threads. Nickelgrass said: If I leave the app open it crashes and the service stops. Click to expand... Post the logs.
Nickelgrass Active Member Licensed User Longtime User Oct 3, 2023 #3 Erel said: It has nothing to do with threads. Click to expand... You are right. It was something else. There was an error in the callback that was only called when the app was not paused. Thanks. Upvote 0
Erel said: It has nothing to do with threads. Click to expand... You are right. It was something else. There was an error in the callback that was only called when the app was not paused. Thanks.