pages

Sunday, September 12, 2021

Android Development - Services 2/4 - Background Tasks Processing


  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.

Otherwise, the app is considered to be running in the foreground. So we use background tasks at the time when the user cannot notice whether our app is working or not.

~~~~

The second one I want to tell you is what way do we need to follow to reach the right path to make users satisfied. So for that I will give you further principles ,too.

Background tasks fall into one of the following main categories:

  • Immediate 
  • Deferred (delayed)
  • Exact (Certain)

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.

this decision tree helps you decide which category is best for
            your background task



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

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.

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.

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.

No comments:

Post a Comment