Reader small image

You're reading from  How to Build Android Apps with Kotlin

Product typeBook
Published inFeb 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781838984113
Edition1st Edition
Languages
Right arrow
Authors (4):
Alex Forrester
Alex Forrester
author image
Alex Forrester

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.
Read more about Alex Forrester

Eran Boudjnah
Eran Boudjnah
author image
Eran Boudjnah

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large-scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.
Read more about Eran Boudjnah

Alexandru Dumbravan
Alexandru Dumbravan
author image
Alexandru Dumbravan

Alexandru Dumbravan has been an Android Developer since 2011 and worked across a variety of Android applications which contained features such as messaging, voice calls, file management, and location. He continues to broaden his development skills while working in London for a popular fintech company.
Read more about Alexandru Dumbravan

Jomar Tigcal
Jomar Tigcal
author image
Jomar Tigcal

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He worked on various stages of app development for small startups to large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada with his wife Celine.
Read more about Jomar Tigcal

View More author details
Right arrow

8. Services, WorkManager, and Notifications

Overview

This chapter will introduce you to the concepts of managing long-running tasks in the background of an app. By the end of this chapter, you will be able to trigger a background task, create a notification for the user when a background task is complete, and launch an application from a notification. This chapter will give you a solid understanding of how to manage background tasks and keep the user informed about the progress of these tasks.

Introduction

In the previous chapter, we learned how to request permissions from the user and use Google's Maps API. With that knowledge, we obtained the user's location and allowed them to deploy an agent on a local map. In this chapter, we will learn how to track a long-running process and report its progress to the user.

We will build an example app where we will assume that Secret Cat Agents (SCAs) get deployed in a record time of 15 seconds. That way, we'll avoid having to wait for very long before our background task completes. When a cat successfully deploys, we will notify the user and let them launch the app, presenting them with a successful deployment message.

Ongoing background tasks are quite common in the mobile world. Background tasks run even when an application is not active. Examples of long-running background tasks include the downloading of files, resource cleanup jobs, playing music, and tracking the user's location. Historically, Google...

Starting a Background Task Using WorkManager

The first question we will address here is, Should we opt for WorkManager or a foreground service? To answer that, a good rule of thumb is to ask; do you need the action to be tracked by the user in real time? If the answer is yes (for example, if you have a task such as responding to the user's location or playing music in the background), then you should use a foreground service, with its attached notification to give the user a real-time indication of state. When the background task can be delayed or does not require user interaction (for example, downloading a large file), use WorkManager.

Note

Starting with version 2.3.0-alpha02 of the WorkManager, you can launch a foreground service via the WorkManager by calling setForegroundAsync(ForegroundInfo). Our control over that foreground service is quite limited. It does allow you to attach a (pre-defined) notification to the work, which is why it is worth mentioning.

In our...

Background Operations Noticeable to the User – Using a Foreground Service

With our SCA all suited up, they are now ready to get to the assigned destination. To track the SCA, we will periodically poll the location of the SCA using a foreground service and update the sticky notification (a notification that cannot be dismissed by the user) attached to that service with the new location. For the sake of simplicity, we will fake the location. Following what you learned in Chapter 7, Android Permissions and Google Maps, you could later replace this implementation with a real one that uses a map.

Foreground services are another way of performing background operations. The name may be a bit counter-intuitive. It is meant to differentiate these services from the base Android (background) services. The former are tied to a notification, while the latter run in the background with no user-facing representation built in. Another important difference between foreground services and...

Summary

In this chapter, we learned how to execute long-running background tasks using WorkManager and foreground services. We discussed how to communicate progress to the user, and how to get the user back into an app once a task is finished executing. All the topics covered in this chapter are quite broad, and you could explore communicating with services, building notifications, and using the WorkManager class further. Hopefully, for most common scenarios, you now have the tools you need. Common use cases include background downloads, the background cleaning up of cached assets, playing music while the app is not running in the foreground, and, combined with the knowledge we gained from Chapter 7, Android Permissions and Google Maps, tracking the user's location over time.

In the next chapter, we will look into making our apps more robust and maintainable by writing unit tests and integration tests. This is particularly helpful when the code you write runs in the background...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
How to Build Android Apps with Kotlin
Published in: Feb 2021Publisher: PacktISBN-13: 9781838984113
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (4)

author image
Alex Forrester

Alex Forrester is an experienced software developer with more than 20 years of experience in mobile, web development, and content management systems. He has been working with Android for over 8 years, creating flagship apps for blue-chip companies across a broad range of industries at Sky, The Automobile Association, HSBC, The Discovery Channel, and O2. Alex lives in Hertfordshire with his wife and daughter. When he's not developing, he likes rugby and running in the Chiltern hills.
Read more about Alex Forrester

author image
Eran Boudjnah

Eran Boudjnah is a developer with over 20 years of experience in developing desktop applications, websites, interactive attractions, and mobile applications. He has been working with Android for about 7 years, developing apps and leading mobile teams for a wide range of clients, from start-ups (JustEat) to large-scale companies (Sky) and conglomerates. He is passionate about board games (with a modest collection of a few hundred games) and has a Transformers collection he's quite proud of. Eran lives in North London with Lea, his wife.
Read more about Eran Boudjnah

author image
Alexandru Dumbravan

Alexandru Dumbravan has been an Android Developer since 2011 and worked across a variety of Android applications which contained features such as messaging, voice calls, file management, and location. He continues to broaden his development skills while working in London for a popular fintech company.
Read more about Alexandru Dumbravan

author image
Jomar Tigcal

Jomar Tigcal is an Android developer with over 10 years of experience in mobile and software development. He worked on various stages of app development for small startups to large companies. Jomar has also given talks and conducted training and workshops on Android. In his free time, he likes running and reading. He lives in Vancouver, Canada with his wife Celine.
Read more about Jomar Tigcal