[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Optimize battery use for task scheduling APIs\n\nThis page suggests a few best practices for setting up well-behaved background\ntasks. These best practices are specifically aimed at reducing battery\nconsumption, but they can also improve device performance in other ways, like\nreducing network use.\n| **Note:** This page focuses on how to optimize tasks created with [WorkManager](/develop/background-work/background-tasks/persistent), our preferred library for scheduling tasks. In many cases, these suggestions also apply to [`JobScheduler`](/reference/android/app/job/JobScheduler) jobs, though the APIs you use are slightly different. Where appropriate, we've called out some corresponding `JobScheduler` APIs.\n\nChoose optimal constraints and combine tasks\n--------------------------------------------\n\nTo minimize the load your tasks place on the device, it's important to [specify\noptimal constraints](/develop/background-work/background-tasks/persistent/getting-started/define-work#work-constraints). (For JobScheduler jobs, see [`JobInfo.Builder`](/reference/android/app/job/JobInfo.Builder#setExpedited(boolean)) for\nthe list of constraints.) To take one example, if you want to make sure your app\ndoesn't run down the battery, it's a good idea to specify the `RequiresCharging`\nconstraint. This constraint tells the system not to run the job unless the\nbattery level is actually increasing. Similarly, using Wi-Fi generally requires\nless power than mobile data, so if your task needs a network connection but can\nwait until an unmetered network is available, it's a good idea to set a\n[`NetworkType.UNMETERED`](/reference/androidx/work/NetworkType#UNMETERED) constraint.\n\nAlso, if you have several similar tasks that would fall under the same\nconstraints, it's usually a good idea to combine them into a single task, so the\ndevice only gets woken up once. For example, suppose your app has three\ndifferent data sets it needs to sync with cloud storage. Instead of scheduling\nthree different tasks--one for each data set--it's usually a better idea to just\nschedule a single \"synchronize the data\" task, define appropriate constraints,\nand let that task do all the pending data synchronization when it runs.\n\nThat said, you shouldn't try to combine *unrelated* tasks into a single\ndo-it-all task. Instead, just make sure to give each task appropriate\nconstraints. For example, if tasks are low priority, make sure to specify that\nthey should run when the device is idle and charging. That way, even if the\ndevice gets woken up several times, it won't hurt the user experience or impact\nbattery life.\n\nOnly mark tasks as expedited when they're time-sensitive\n--------------------------------------------------------\n\nIf a task is particularly urgent, you can [mark it as expedited](/develop/background-work/background-tasks/persistent/getting-started/define-work#expedited). (For\nJobScheduler jobs, call [`JobInfo.Builder.setExpedited(true)`](/reference/android/app/job/JobInfo.Builder#setExpedited(boolean)).) Doing so\nprioritizes the task in a number of ways. For example, the system runs those\ntasks immediately when it can, and power management restrictions are less likely\nto affect expedited tasks.\n\nFor these reasons, you should be careful to *only* mark a task as expedited when\nyou need to. Because expedited tasks can override some system efficiencies,\nexpedited tasks can drain more power than they would if they weren't marked that\nway.\n\nYou should only mark a task as expedited if it's time-sensitive, and the user\nexperience would be impaired if the task took longer to execute. For example, if\nyour app runs a task to handle a high-priority FCM message, that's an\nappropriate reason to mark the task as expedited. But you shouldn't mark a task\nas expedited just to override system optimizations.\n\nCheck why your tasks were stopped\n---------------------------------\n\nIf your tasks stop before they finish, you can check why they were stopped by\ncalling [`WorkInfo.getStopReason()`](/reference/androidx/work/WorkInfo#getStopReason()). (For JobScheduler jobs, call\n[`JobParameters.getStopReason()`](/reference/android/app/job/JobParameters#getStopReason()) It's important to do this for a couple of\nreasons. First of all, of course, you want your tasks to finish. Finding out why\nyour tasks stopped helps you avoid similar situations. But also, the system is\nlikely to stop tasks because of behavior that overuses system resources. You\ndon't want your app to be a bad citizen, using the battery or network\nunnecessarily.\n\nFor example, if your tasks frequently get stopped with the reason\n[`STOP_REASON_TIMEOUT`](/reference/androidx/work/WorkInfo#STOP_REASON_TIMEOUT()), there might be an edge case that sometimes causes\nyour tasks to take much longer than you're expecting.\n\nWe recommend that you use your analytics engine to track whether your app's\ntasks are stopped, and for what reasons.\n| **Important:** On Android 14 or higher, if an app's tasks time out too often, the system can [put the app in the restricted standby bucket](/about/versions/14/behavior-changes-all#triggers-to-restricted-bucket)."]]