Android WorkManager Stable Release
Written by Mike James   
Wednesday, 06 March 2019

Jetpack is a collection of libraries that should make Android easier to use. The problem is what to do while they are maturing to a usable state. The good news is that WorkManager is now considered stable.

jetpackicon

Android programming is a funny mix of Java programming with lots of extras and alternatives. One of the problems in coping with it is that the API is a huge sprawling mess and finding out what the best way to do something is usually difficult and occasionally impossible. In particular, there are a number of ways of creating a multi-threaded program especially, one that needs to run in the background. At this point you might well say, well yes it's a complicated and sophisticated problem, but as the Android GUI is single-threaded, like most GUIs, it is essential that for all but the simplest app you move to multiple threads. Something so commonplace should not be so hard.

Enter WorkManager 1.0, a solution to nearly all our multi-tasking problems. It is easy to use. Simply create a sub-class of Worker and start customizing it by overriding doWork. You can set constraints on how you want the Worker to be run - power, network availability etc. The ability to specify the conditions needed to run is the biggest simplification. Your instance of Worker will  be run automatically and restarted if it has to be suspended for any reason. You can set a one-off request or a periodic scheduling.

You can discover the "company" line from the following promo video which is quite a good introduction to WorkManager:

It is a good introduction, but I'd like to draw your attention to the flow diagram shown briefly early in the video:

background

Still a bit of a complicated mess, isn't it? And what is worse if you look up Foreground service you will find it even more confusing:

"If you must perform work outside of your main thread, but only while the user is interacting with your application, you should instead create a new thread. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), and stop it in onStop(). Also consider using AsyncTask or HandlerThread instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations."

If you come from a Java background then you might recognize the low-level threading commands but what about the rest? You also now have to throw in Kotlin coroutines as a possible solution.

jetpackxon2

It is not that I think that WorkManager is a bad thing - it is going to save a lot of boilerplate code and mistakes. What I am complaining about is that you could consider it very late and very little. Android isn't a new platform and you really do get the impression that there is no one in charge.

Oh, for a Benevolent Dictator For Life.androidworkman

 

More Information

Related Articles

Android Studio 3.3 - Trying Harder?

Android Development - Constant Churn

Android Studio 3.2 Released - More Cruft Than Substance

JetPack And Android Studio 3.2 - Not Much New

Android's Uncertain Future

Chromebook Tablets - The End of Android?

Android Studio 3.1 Released - Widgets Lost

Android KTX - Kotlin Extensions

Kotlin - New Language For Android

Android Studio Moves Toward Java 8

Google's Jack & Jill Android Java Compiler Project Is Dead

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


Python 3.13 Is Here
09/10/2024

As time ticks on, the changes to the Python language become fewer and this makes it easier to upgrade. With this release the emphasis is on performance rather than new features.



GitHub Universe AI Announcements - Copilot And Spark
30/10/2024

GitHub has announced several improvements for developers at Universe, its annual conference. Developers will get multi-model Copilot and GitHub Spark, an AI-native tool for building applications in na [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Wednesday, 06 March 2019 )