Background tasks : Overview

Dmitry Klokov·2021년 1월 2일
0

ANDROID-CORE-TOPICS

목록 보기
1/1
post-thumbnail

Guide to background processing | Android Developers

Guide to background processing

Processing data in the background is an important part of creating an Android application that is both responsive for your users as well as a good citizen on the Android platform. This guide explains what qualifies as background work, defines background task categories, provides you with criteria to categorize your tasks, and recommends APIs that you should use to execute them.

Guiding principle


An app is considered to be running in the background as long as each of the following conditions are satisfied:

  1. None of the app's activities are currently visible to the user.
  2. The app isn't running any foreground services that started while an activity from the app was visible to the user.

The following list shows common pending tasks that an app manages while it runs in the background:

Categories of background tasks


Background tasks fall into one of the following main categories:

  • Imeediate
  • Deferred
  • Exact

To categorize a task, answer the following questions, and traverse the corresponding decision tree in figure 1:

Does the task need to complete while the user is interacting with the application?

If so, this task should be categorized for immediate execution. If not, proceed to the second question.

Does the task need to run at an exact time?

If you do need to run a task at an exact time, categorize the task as exact.

Most tasks don't need to be run at an exact time. Tasks generally allow for slight variations in when they run that are based on conditions such as network availability and remaining battery. Tasks that don't need to be run at an exact time should be categorized as deferred.


The following sections describe recommended solutions for each background task type.

Immediate tasks

We recommend Kotlin coroutines for tasks that should end when the user leaves a certain scope or finishes an interaction. Many Android KTX libraries contain ready-to-use coroutine scopes for common app components like ViewModel and common application lifecycles.

For Java programming language users, see Threading on Android for recommended options.

For tasks that should be executed immediately and need continued processing, even if the user puts the application in background or the device restarts, we recommend using WorkManager and its support for long-running tasks.

In specific cases, such as with media playback or active navigation, you might want to use foreground Services directly.

Deferred tasks

Every task that is not directly connected to a user interaction and can run at any time in the future can be deferred. The recommended solution for deferred tasks is WorkManager.

WorkManager makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts. See the documentation for WorkManager to learn how to schedule these types of tasks.

Exact tasks

A task that needs to be executed at an exact point in time can use AlarmManager.

To learn more about AlarmManager, see Schedule repeating alarms.

profile
Power Weekend

0개의 댓글