Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
How to Build Android Apps with Kotlin

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

Product type Book
Published in Feb 2021
Publisher Packt
ISBN-13 9781838984113
Pages 794 pages
Edition 1st Edition
Languages
Authors (4):
Alex Forrester Alex Forrester
Profile icon Alex Forrester
Eran Boudjnah Eran Boudjnah
Profile icon Eran Boudjnah
Alexandru Dumbravan Alexandru Dumbravan
Profile icon Alexandru Dumbravan
Jomar Tigcal Jomar Tigcal
Profile icon Jomar Tigcal
View More author details

Table of Contents (17) Chapters

Preface
1. Creating Your First App 2. Building User Screen Flows 3. Developing the UI with Fragments 4. Building App Navigation 5. Essential Libraries: Retrofit, Moshi, and Glide 6. RecyclerView 7. Android Permissions and Google Maps 8. Services, WorkManager, and Notifications 9. Unit Tests and Integration Tests with JUnit, Mockito, and Espresso 10. Android Architecture Components 11. Persisting Data 12. Dependency Injection with Dagger and Koin 13. RxJava and Coroutines 14. Architecture Patterns 15. Animations and Transitions with CoordinatorLayout and MotionLayout 16. Launching Your App on Google Play

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 2021 Publisher: Packt ISBN-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.
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 £13.99/month. Cancel anytime}