There are many tasks in an Android which an Android developer would like to do or probably the recommended way of executing certain tasks is in the background. The most common and efficient way to do this was to execute the tasks in a background service. But Google introduced many limitations because of which, services were almost deprecated although not officially. I’ll explain why I mention deprecated.

The limitations on services running in background were introduced for a range of reasons but most common being the issue of battery consumption. Many apps would run the service till the app was installed without any check if it was actually needed. This led to memory consumption and battery consumption issues on the device. To fight this or rather limit this, Doze Mode was introduced, starting with Marshmallow and then followed by Nougat.

Doze Mode

Doze Mode in a nutshell is a mode which starts when the user switches off the screen and ends when the screen is switched on or the phone is connected to the charger. During this period the doze mode disables the Network, Syncs, GPS, alarms and Wi-Fi scans.

So now, if there is an app using services in the background and the device is in doze mode, starting a service would throw an exception, because the service is not allowed to run during doze mode. So unless, the app is in foreground or not killed, the service component was rendered not so useful. The service component was restricted from doing what it was primarily used for, namely performing long running applications in the background, whether or not the app is being used actively. This is why i say that service component is almost deprecated although not officially. But to make things easier for android developers, JobSchedulers and then WorkManger was introduced.

JobSchedulers

JobSchedulers were introduced in API 21. They were meant for scheduling various types of jobs against the framework that will be executed in your application’s own process.But this release had bugs which rendered it not so useful for its immediate usage in apps. The most im[ortant bug which was reolved in API 233 was that the jobs would not be scheduled after the device reboot. So basically one could use Job Schedulers efficiently only after API level 23. But again implementing Jobs schedulers has one another constraint. It requires Google play services. So apps which used other app stores would not be able to implement it.

So the last resort to successfully and efficiently implementing a task in the background while the app is not being actively used was to implement the WorkManagers

WorkManager

Google introduced Workmanger as a part of Jetpack Library.

WorkManager aims to simplify the developer experience by providing a first-class API for system-driven background processing. It is intended for background jobs that should run even if the app is no longer in the foreground. Where possible, it uses JobScheduler or Firebase JobDispatcher to do the work; if your app is in the foreground, it will even try to do the work directly in your process.

Some of the benefits work manager has to offer are as follows

  • Provides tasks which can survive process death
  • Can Schedule a onetime task or Periodic ones.
  • It can wake up the app’s process to execute the code & therefore guarantees that works will be executed.
  • Add constraints before executing a task
  • You can observe work status and chain several work in a particular order
  • Chaining allows to segregate big chunk of work into small parts and execute them based on different constraints
  • Manages doze mode or other restrictions imposed by OS.

So given the above benefits, for now, the recommended way of carrying out long running tasks without blocking the main thread is scheduling Work for Work Manager. The below diagram shows how Workmanager works across API Levels.

For more information on Workmanger one can visit the android developers page – https://developer.android.com/topic/libraries/architecture/workmanager

LEAVE A REPLY

Please enter your comment!
Please enter your name here