Home > Software design >  Are scheduled threads runnable after app termination on Android?
Are scheduled threads runnable after app termination on Android?

Time:08-24

In our Android Application - during app start we schedule threads to run every 8 hours for transmitting files. I have been observing inconsistent transmission behavior when the app is terminated after app start (i.e. the user kills the app). I am suspecting that the scheduled threads are somehow cancelled/destroyed/terminated with the app gets terminated. Does anyone know whether this is the case on Android?

Sample Code of how I am deploying scheduled background threads.

scheduledExecutor = new ScheduledThreadPoolExecutor(1, <this param is a class that Creates threads with the default priority set to background.>);

scheduledExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());

scheduledExecutor.schedule(new Handler(), 28800000, TimeUnit.MILLISECONDS);

CodePudding user response:

during app start we schedule threads to run every 8 hours for transmitting files

That will not work very well.

I have been observing inconsistent transmission behavior when the app is terminated after app start (i.e. the user kills the app)

You will have inconsistent transmission behavior if the app is in the background as well. The only scenario in which what you describe would work is some sort of kiosk mode app or other situation where your app UI is always in the foreground.

I am suspecting that the scheduled threads are somehow cancelled/destroyed/terminated with the app gets terminated

Android will terminate your app's process after a period of time, to free up system RAM for other apps. At that point, your threads and other CPU/RAM structures all go away. This is covered in the documentation, as well as many books and courses on Android app development.

And, for the purposes of transmitting files, your app's ability to use the Internet will be affected by Doze mode, app standby, and manufacturer-specific battery-conservation approaches. Doze mode is covered in the documentation.

The standard recommendation today is to use WorkManager, where you schedule the work with instructions that you need network access when the work is performed.

  • Related