How to Upload Background to Android Studio
Processing information in the background is an important office of creating an Android application that is both responsive for your users likewise as a proficient citizen on the Android platform. Doing work on the main thread can lead to poor functioning and therefore a poor user experience.
This guide explains what qualifies as background work, defines background job categories, provides you with criteria to categorize your tasks, and recommends APIs that you should use to execute them.
Guiding principle
In general, y'all should take any blocking tasks off the UI thread. Common long-running tasks include things like decoding a bitmap, accessing storage, working on a machine learning (ML) model, or performing network requests.
Definition of background work
An app is running in the background when both the following conditions are satisfied:
- None of the app'due south activities are currently visible to the user.
- The app isn't running whatever foreground services that started while an activeness from the app was visible to the user.
Otherwise, the app is running in the foreground.
Common types of groundwork work
Groundwork work falls into one of 3 primary categories:
- Firsthand: Needs to execute right away and complete presently.
- Long Running: May take some time to consummate.
- Deferrable: Does not need to run right away.
Likewise, groundwork work in each of these three categories can exist either persistent or impersistent:
- Persistent work: Remains scheduled through app restarts and device reboots.
- Impersistent work: No longer scheduled afterward the procedure ends.
Approaches to groundwork work
You lot should arroyo impersistent or persistent piece of work differently:
- All persistent work: You should utilize WorkManager for all forms of persistent work.
- Immediate impersistent piece of work: You should use Kotlin coroutines for firsthand impersistent work. For Java programming language users, read the guide on threading for recommended options.
- Long-running and deferrable impersistent work: You should not apply long-running and deferrable impersistent work. You should instead complete such tasks through persistent work using WorkManager.
The following tabular array lays out which approach you should take for each type of background work.
Category | Persistent | Impersistent |
---|---|---|
Immediate | WorkManager | Coroutines |
Long running | WorkManager | Not recommended. Instead, perform piece of work persistently using WorkManager. |
Deferrable | WorkManager | Not recommended. Instead, perform piece of work persistently using WorkManager. |
Immediate work encompasses tasks which need to execute right away. These are tasks which are of import to the user or which you otherwise can't schedule for deferred execution at a later time. They are of import enough that they might need to remain scheduled for prompt execution even if the app closes or the device restarts.
Recommended solution
For persistent immediate work, yous should use WorkManager with a OneTimeWorkRequest. Expedite a WorkRequest with setExpedited()
.
For impersistent firsthand work, you should you use Kotlin coroutines. If your app uses the Coffee programming language, you lot should utilise RxJava or Guava. You lot can also utilize Executors.
Examples
- An app needs to load data from a data source. However, making such a request on the main thread will block it and cause UI jank. The app instead makes the request off the primary thread in a coroutine.
- An app needs to transport a bulletin in a chat app. The app creates a
Worker
and enqueues the chore every bit aWorkRequest
. It expedites theWorkRequest
withsetExpedited()
.
Long-running work
Work is long running if it is likely to have more than than 10 minutes to complete.
Recommended solution
WorkManager allows yous to handle such tasks using a long-running Worker
.
Example
An app needs to download a big file which yous tin non chunk. Information technology creates a long-running Worker
and enqueues the download. The app and then downloads the file in the background over fifteen minutes.
Deferrable work
Deferrable work is whatever work that does not need to run right away.
Recommended solution
Scheduling deferred piece of work through WorkManager is the best mode to handle tasks that don't demand to run immediately but which ought to remain scheduled when the app closes or the device restarts.
Example
An app wants to regularly sync data with a backend. The user does not trigger the sync, and the work should take place when the device is idle. The recommended approach is to apply a PeriodicWorkRequest
with a custom Worker
and constraints for these scenarios.
Alarms
Alarms are a special utilise case that are not a part of background work. You should execute groundwork work through the two solutions outlined above, coroutines and WorkManager.
You lot should only use AlarmManager just for scheduling verbal alarms such as alarm clocks or calendar events. When using AlarmManager to schedule background piece of work, information technology wakes the device from Doze mode and its utilise can therefore have a negative impact on battery life and overall system wellness. Your app will be responsible for such impacts.
Replacing foreground services
Android 12 restricts launching foreground services from the background. For most cases, you lot should utilise setForeground()
from WorkManager rather than handle foreground services yourself. This allows WorkManager to manage the lifecycle of the foregound service, ensuring efficiency.
You notwithstanding should utilise foreground services to perform tasks that are long running and need to notify the user that they are ongoing. If you utilize foreground services directly, ensure you close down the service correctly to preserve resources efficiency.
Some use cases for using foreground services directly are as follows:
- Media playback
- Activity tracking
- Location sharing
- Voice or video calls
Source: https://developer.android.com/guide/background