Skip to content

Most visited

Recently visited

navigation

Intelligent Job-Scheduling

Modern apps can perform many of their tasks asynchronously, outside the direct flow of user interaction. Some examples of these asynchronous tasks are:

Scheduling this work intelligently can improve your app’s performance, along with aspects of system health such as battery life. JobScheduler does this scheduling work for you.

There are several APIs that your app can use to schedule background work. Chief among these options is JobScheduler. The JobScheduler API allows you to specify robust conditions for executing tasks, along with centralized task scheduling across the device for optimal system health. JobScheduler also offers highly scalable functionality: it is suitable for small tasks like clearing a cache, and for large ones such as syncing a database to the cloud.

In addition to JobScheduler, there are several other facilities available to help your app schedule work. These include:

This page provides brief introductions to JobScheduler and other APIs that can help your app schedule work to maximize app performance and system health.

Android Framework JobScheduler

JobScheduler is the Android framework API for scheduling tasks or work. It first became available in Android 5.0 (API level 21), and remains under active development. Notably, Android 7.0 (API level 24) added the ability to trigger jobs based on ContentProvider changes.

JobScheduler is implemented in the platform, which allows it to collect information about jobs that need to run across all apps. This information is used to schedule jobs to run at, or around, the same time. Batching job execution in this fashion allows the device to enter and stay in sleep states longer, preserving battery life.

You use JobScheduler by registering jobs, specifying their requirements for network and timing. The system then gracefully schedules the jobs to execute at the appropriate times. At the same time, it also defers job execution as necessary to comply with Doze and App Standby restrictions. JobScheduler provides many methods to define job-execution conditions.

If your app targets Android 5.0 (API level 21), we recommend that you use the JobScheduler to execute background tasks. For more information about JobScheduler, see its API-reference documentation.

AlarmManager

The AlarmManager API is another option that the framework provides for scheduling tasks. This API is useful in cases in which an app needs to post a notification or set off an alarm at a very specific time.

You should only use this API for tasks that must execute at a specific time, but do not require the other, more robust, execution conditions that JobScheduler allows you to specify, such as device idle and charging detect.

Firebase JobDispatcher

Firebase JobDispatcher is an open-source library that provides an API similar to JobScheduler in the Android platform. Firebase JobDispatcher serves as a JobScheduler-compatibility layer for apps targeting versions of Android lower than 5.0 (API level 21).

Firebase JobDispatcher supports the use of Google Play services as an implementation for dispatching (running) jobs, but the library also allows you to define and use other implementations: For example, you might decide to use JobScheduler or write your own, custom code. Because of this versatility, we recommend that you use this Firebase JobDispatcher if your app targets a version of Android lower than 5.0 (API level 21).

For more information about Firebase JobDispatcher, refer to its documentation and source code.

Additional Facilities

In addition to the APIs and libraries described above, there are also sync adapters and services that can enable your app, under specific conditions, to perform better and more robustly.

SyncAdapter

The framework continues to provide the SyncAdapter class for managing tasks that sync data between the device and a server. Sync adapters are designed specifically for syncing data between a device and the cloud; you should only use them for this type of task. Sync adapters are more complex to implement than the libraries and APIs mentioned above, because they require at least a fake authenticator and content provider implementation. For these reasons, you typically should not create a sync adapter just to sync data to the cloud in the background. Wherever possible, you should instead use JobScheduler, Firebase JobDispatcher, or GCM Network Manager .

In Android N (API level 24), the SyncManager sits on top of the JobScheduler. You should only use the SyncAdapter class if you require the additional functionality that it provides.

Services

The Services framework allows you to perform long-running operations in the background. We recommend foreground services for tasks, such as playing music, which need to stay resident for the user. Bound services also continue to be useful for various use cases: for example, when a service needs to run only when a user is viewing a fragment or activity.

You should avoid using started services that run perpetually or perform periodic work, since they continue to use device resources even when they are not performing useful tasks. Instead, you should use other solutions that this page describes, and that provide native lifecycle management. Use started services only as a last resort. The Android platform may not support started services in the future.

Additional Points

Regardless of the solution you adopt, keep the following points in mind:

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)