1 page.title=Intelligent Job-Scheduling 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#js">Android Framework JobScheduler</a></li> 10 <li><a href="#am">AlarmManager</a></li> 11 <li><a href="#fjd">Firebase JobDispatcher</a></li> 12 <li><a href="#gcmnm">GCM Network Manager</a></li> 13 <li><a href="#af">Additional Facilities</a></li> 14 <ul> 15 <li><a href="#sa">SyncAdapter</a></li> 16 <li><a href="#services">Services</a></li> 17 </ul> 18 <li><a href="#ap">Additional Points</a></li> 19 </ol> 20 21 </div> 22 </div> 23 24 <p> 25 Modern apps can perform many of their tasks asynchronously, outside the 26 direct flow of user interaction. Some examples of these asynchronous tasks are: 27 </p> 28 29 <ul> 30 <li>Updating network resources.</li> 31 <li>Downloading information.</li> 32 <li>Updating background tasks.</li> 33 <li>Scheduling system service calls.</li> 34 </ul> 35 36 <p> 37 Scheduling this work intelligently can improve your apps performance, 38 along with aspects of system health such as battery life. {@link android.app.job.JobScheduler} 39 does this scheduling work for you. 40 <p> 41 42 <p> 43 There are several APIs that your app can use to schedule background work. Chief 44 among these options is {@link android.app.job.JobScheduler}. 45 The {@link android.app.job.JobScheduler} API allows you to specify robust 46 conditions for executing tasks, 47 along with centralized task scheduling across the device for optimal 48 system health. {@link android.app.job.JobScheduler} also offers highly 49 scalable functionality: it is suitable for small tasks like clearing a cache, 50 and for large ones such as syncing a database to the cloud. 51 <p> 52 53 <p> 54 In addition to {@link android.app.job.JobScheduler}, there are several other 55 facilities available to help your app schedule work. These include: 56 </p> 57 58 <ul> 59 <li><a href="{@docRoot}reference/android/app/AlarmManager.html">AlarmManager 60 </a></li> 61 <li><a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 62 Firebase JobDispatcher</a></li> 63 <li><a href="https://developers.google.com/cloud-messaging/network-manager"> 64 GCM Network Manager</a></li> 65 <li><a href="{@docRoot}reference/android/content/AbstractThreadedSyncAdapter.html"> 66 SyncAdapter</a></li> 67 <li><a href="{@docRoot}reference/android/content/AbstractThreadedSyncAdapter.html"> 68 Additional Facilities</a></li> 69 </ul> 70 71 <p> 72 This page provides brief introductions to {@link android.app.job.JobScheduler} 73 and other APIs that can help your app schedule work to maximize app performance 74 and system health. 75 </p> 76 77 <h2 id="js">Android Framework JobScheduler</h2> 78 79 <p> 80 {@link android.app.job.JobScheduler} is the Android frameworks native platform 81 for scheduling tasks or work. It first became available in Android 5.0 (API 82 level 21), and remains under active development. Notably, Android N (API 83 level 24) added the ability to trigger jobs based on 84 {@link android.content.ContentProvider} changes. 85 </p> 86 87 <p> 88 {@link android.app.job.JobScheduler} is implemented in the platform, which 89 allows it to collect information about jobs that need to run across all apps. 90 This information is used to schedule jobs to run at, or around, the same time. 91 Batching job execution in this fashion allows the device to enter and stay in 92 sleep states longer, preserving battery life. 93 </p> 94 95 <p> 96 You use {@link android.app.job.JobScheduler} by registering jobs, specifying 97 their requirements for network and timing. The system then gracefully schedules 98 the jobs to execute at the appropriate times. At the same time, it also defers 99 job execution as necessary to comply with 100 <a href="{@docRoot}topic/performance/monitoring-device-state/doze-standby.html"> 101 Doze and App Standby</a> restrictions. 102 {@link android.app.job.JobScheduler} provides many methods to define 103 job-execution conditions. 104 </p> 105 106 <p> 107 If your app targets Android 5.0 (API level 21), we recommend that you use the 108 {@link android.app.job.JobScheduler} to execute background tasks. For more 109 information about {@link android.app.job.JobScheduler}, see its 110 <a href="{@docRoot}reference/android/app/job/JobScheduler.html">API-reference 111 documentation.</a> 112 </p> 113 114 <h2 id="am">AlarmManager</h2> 115 116 <p> 117 The {@link android.app.AlarmManager} API is another option that the framework 118 provides for scheduling tasks. This API is useful in cases in which an app 119 needs to post a notification or set off an alarm at a very specific time. 120 </p> 121 122 <p> 123 You should only use this API for tasks that must execute at a specific time, 124 but do not require the other, more robust, execution conditions that 125 {@link android.app.job.JobScheduler} allows you to specify, such as device 126 idle and charging detect. 127 </p> 128 129 <p> 130 {@link android.app.AlarmManager} does not honor 131 <a href="{@docRoot}topic/performance/monitoring-device-state/doze-standby.html"> 132 Doze and App Standby</a> restrictions; it runs tasks even during 133 <a href="{@docRoot}topic/performance/monitoring-device-state/doze-standby.html"> 134 Doze or App Standby</a> mode. Additionally, 135 an app running during 136 <a href="{@docRoot}topic/performance/monitoring-device-state/doze-standby.html"> 137 Doze or App Standby</a> mode cannot use the network. 138 </p> 139 140 <h2 id="fjd">Firebase JobDispatcher</h2> 141 142 <p> 143 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 144 Firebase JobDispatcher</a> is an open-source library that provides an API similar to 145 {@link android.app.job.JobScheduler} in the Android platform. 146 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 147 Firebase JobDispatcher</a> serves as a 148 {@link android.app.job.JobScheduler}-compatibility layer for apps targeting 149 versions of Android lower than 5.0 (API level 21). 150 </p> 151 152 <p> 153 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 154 Firebase JobDispatcher</a> supports the use of Google Play services as an 155 implementation for dispatching (running) jobs, but the library also allows you 156 to define and use other implementations: For example, you might decide to use 157 {@link android.app.job.JobScheduler} or write your own, custom code. 158 Because of this versatility, we recommend that you use this 159 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 160 Firebase JobDispatcher</a> if your app targets a version of Android lower 161 than 5.0 (API level 21). 162 </p> 163 164 <p> 165 For more information about 166 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 167 Firebase JobDispatcher</a>, refer to its 168 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 169 documentation and source code</a>. 170 </p> 171 172 <h2 id = "gcmnm">GCM Network Manager</h2> 173 174 <p> 175 <a href="https://developers.google.com/cloud-messaging/network-manager">GCM 176 Network Manager</a> is a compatibility port of the Android 177 {@link android.app.job.JobScheduler} for apps that support versions of Android 178 prior to 5.0 (API level 21). This API has all of the same advantages as 179 {@link android.app.job.JobScheduler}. However, it depends on Google Play 180 services, which acts like {@link android.app.job.JobScheduler} by taking care 181 of cross-app job scheduling to improve battery life. This client library is 182 part of the Google Play services GCM client library, but does not require 183 that an app use 184 <a href="https://developers.google.com/cloud-messaging/">Google Cloud 185 Messaging</a>. 186 </p> 187 188 <p> 189 This library is an earlier version of 190 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 191 Firebase JobDispatcher</a>. Forward development on GCM Network Manager has 192 stopped; we recommend that you use 193 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 194 Firebase JobDispatcher</a> going forward. 195 </p> 196 197 <h2 id="af">Additional Facilities</h2> 198 199 <p> 200 In addition to the APIs and libraries described above, there are also sync 201 adapters and services that can enable your app, under specific conditions, 202 to perform better and more robustly. 203 </p> 204 205 <h3 id="sa">SyncAdapter</h3> 206 207 <p> 208 The framework continues to provide the 209 {@link android.content.AbstractThreadedSyncAdapter SyncAdapter} class for 210 managing tasks that sync data between the device and a server. Sync adapters are 211 designed specifically for syncing data between a device and the cloud; you 212 should only use them for this type of task. Sync adapters are more complex to 213 implement than the libraries and APIs mentioned above, because they require at 214 least a fake 215 <a href="{@docRoot}training/sync-adapters/creating-authenticator.html"> 216 authenticator</a>and 217 <a href="{@docRoot}training/sync-adapters/creating-stub-provider.html"> 218 content provider</a> implementation. For these reasons, you typically should 219 not create a sync adapter just to sync data to the cloud in the 220 background. Wherever possible, you should instead use 221 {@link android.app.job.JobScheduler}, 222 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 223 Firebase JobDispatcher</a>, or 224 <a href="https://developers.google.com/cloud-messaging/network-manager">GCM 225 Network Manager</a> . 226 </p> 227 228 <p> 229 In Android N (API level 24), the {@code SyncManager} sits on top of 230 the {@link android.app.job.JobScheduler}. You should only use the 231 {@link android.content.AbstractThreadedSyncAdapter SyncAdapter} 232 class if you require the additional functionality that it provides. 233 </p> 234 235 <h3 id="services">Services</h3> 236 237 <p> 238 The <a href="{@docRoot}guide/components/services.html">Services</a> 239 framework allows you to perform long-running operations in the background. 240 We recommend foreground services for tasks, such as playing 241 music, which need to stay resident for the user. Bound services also continue 242 to be useful for various use cases: for example, when a service needs to 243 run only when a user is viewing a fragment or activity. 244 </p> 245 246 <p> 247 You should avoid using started services that run perpetually or 248 perform periodic work, since they continue to use device resources 249 even when they are not performing useful tasks. Instead, you 250 should use other solutions that this page describes, 251 and that provide native lifecycle management. Use started services 252 only as a last resort. The Android platform may not support 253 started services in the future. 254 </p> 255 256 <h2 id="ap">Additional Points</h2> 257 258 <p> 259 Regardless of the solution you adopt, keep the following points in mind: 260 </p> 261 262 <ul> 263 <li>Captive Internet Portals, VPNs, and proxies can pose 264 Internet-connectivity detection problems. A library or API may think the 265 Internet is available, but your service may not be accessible. Fail 266 gracefully and reschedule as few of your tasks as possible.</li> 267 <li>Depending on the conditions you assign for running a task, 268 such as network availability, after the task is triggered, a 269 change may occur so that those conditions are no longer met. 270 In such a case, your operation may fail unexpectedly and repeatedly. 271 For this reason, you 272 should code your background task logic to notice when tasks are failing 273 persistently, and perform exponential back-off to avoid 274 inadvertently over-using resources.</li> 275 276 277 278 <li>Remember to use exponential backoff when rescheduling any work, 279 especially when using {@link android.app.AlarmManager}. If your app uses 280 {@link android.app.job.JobScheduler}, 281 <a href="https://developers.google.com/cloud-messaging/network-manager">GCM 282 Network Manager</a>, 283 <a href="https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-"> 284 Firebase JobDispatcher</a>, or sync adapters, exponential backoff is automatically used.</li> 285 </ul> 286