Processing data in the background is an important part of creating Android applications that are responsive to users and compliant with the policies and regulations of the Android platform.

Category of background tasks

Background tasks fall into the following main categories:

  • Real-time tasks
  • Delayed tasks
  • Precise task

To categorize tasks, answer the following questions and traverse the corresponding decision tree in Figure 1:

  • Do tasks need to be completed while the user is interacting with the application?

    If so, the task should be classified as an immediate task. If not, please proceed to the second question.

  • Do tasks need to run at precise points in time?

    If you really need to run a task at a precise point in time, you should classify the task as a precise task.

Most tasks don’t need to run at precise points in time. In general, the point at which tasks are allowed to run varies slightly, depending on conditions such as network availability and power remaining. Tasks that do not need to run at a precise point in time should be classified as deferred tasks.

Recommended solution

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

Real-time tasks

For tasks that should end when the user leaves a particular scope or completes an interaction, we recommend using Kotlin coroutines. Many Android KTX libraries contain off-the-shelf coroutine scopes for common application components (such as ViewModel) and common application lifecycles.

If you are a user of the Java programming language, you can use threads and use handlers to communicate with the main thread

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

Under certain circumstances (such as when using media playback or active navigation), you may want to use the foreground service directly.

Delayed tasks

Tasks that are not directly related to user interaction and can be run at any time in the future can be postponed. The WorkManager solution is recommended for deferred tasks.

If you want some deferred asynchronous tasks to work even after the application exits or the device restarts, you can easily schedule them using WorkManager.

Precise task

AlarmManager can be used for tasks that need to be executed at precise points in time.

The WorkManager overview of

WorkManager is an API that makes it easy to schedule reliable asynchronous tasks that should run even after you exit the application or restart the device. The WorkManager API is a suitable and recommended replacement for all previous Android backend scheduling apis (including FirebaseJobDispatcher, GcmNetworkManager, and JobScheduler). WorkManager incorporates the functionality of its predecessor in its modern, consistent API, which supports API Level 14 and was developed with the impact on battery life in mind.

In the background, WorkManager uses underlying jobs to schedule services based on the following conditions:

The advantage of the WorkManager

In addition to having a simpler and more consistent API, WorkManager offers a number of other key advantages, including:

Work constraints

Use work constraints to clearly define the best conditions under which work will run. (For example, run only when the device is connected to a Wi-Fi network, when the device is idle, or when there is enough storage space.)

Powerful scheduling

WorkManager allows you to schedule work using flexible scheduling Windows to run one-off or repetitive work. You can also tag or name work to schedule unique, replaceable work and to monitor or cancel workgroups. The scheduled work is stored in an internally managed SQLite database, and it is the responsibility of the WorkManager to ensure that the work continues and is rescheduled after the device restarts. In addition, WorkManager follows power-saving features and best practices such as low power consumption patterns, so you don’t have to worry about this.

Flexible retry policy

Sometimes work fails. WorkManager provides flexible retry policies, including a configurable exponential retreat policy.

Work chain

For complex, related work, you can use smooth, natural interfaces to string together work tasks so you can control which parts run in sequence and which in parallel.

WorkManager.getInstance(...)
    .beginWith(Arrays.asList(workA, workB))
    .then(workC)
    .enqueue();
Copy the code

For each work task, you can define the input and output data for the work. When you cascade the work together, the WorkManager automatically passes the output data from one work task to the next.

Built-in thread interoperability

WorkManager seamlessly integrates RxJava and coroutines, and has the flexibility to plug in your own asynchronous apis.

Reference: developer. The android. Google. Cn/guide/backg… Developer. The android. Google. Cn/topic/libra…