最佳化背景活動Optimize background activity

通用 Windows app 應該以一致的方式在所有裝置系列上正常執行。Universal Windows apps should perform consistently well across all device families. 在電池供電的裝置上,耗用電力是影響使用者對您 app 整體體驗的重要因素。On battery-powered devices, power consumption is a critical factor in the user's overall experience with your app. 全天候的電池使用時間對每位使用者而言是令人滿意的功能,但還是需要裝置上安裝的所有軟體 (包括您自己的軟體) 所提供的效率。All-day battery life is a desirable feature to every user, but it requires efficiency from all of the software installed on the device, including your own.

在應用程式的總能源成本方面,背景工作行為可以說是最重要的因素。Background task behavior is arguably the most important factor in the total energy cost of an app. 背景工作是任何已向系統註冊且不需開啟 app 就能執行的程式活動。A background task is any program activity that has been registered with the system to run without the app being open. 如需詳細資訊,請參閱建立及註冊跨處理序的背景工作See Create and register an out-of-process background task for more information.

背景活動權限Background activity permissions

在執行 Windows 10 版本 1607 或更新版本的傳統型和行動裝置上,使用者可以在 [設定] 應用程式的 [電池] 區段中,檢視其 [依 App 的電池使用情況]。On desktop and mobile devices running Windows 10, version 1607 or later, users can view their "Battery usage by app" in the Battery section of the Settings app. 他們將會在這裡看到應用程式清單,以及每個應用程式已耗用的電池使用時間百分比 (自上次充電以來已使用的電池使用時間量)。Here they will see a list of apps and the percentage of battery life that each app has consumed (out of the amount of battery life that has been used since the last charge). 對於這份清單上的 UWP 應用程式,使用者可以選取應用程式,以開啟與背景活動相關的控制項。For UWP apps on this list, users can select the app to open up controls related to background activity.

依 App 的電池使用情況

行動裝置的背景權限Background permissions on mobile

在行動裝置上,使用者會看到選項按鈕的清單,指定該應用程式的背景工作權限設定。On mobile devices, users will see a list of radio buttons that specify the background task permission setting for that app. 背景活動可以設定為 [一律允許]、[永不允許] 或 [由 Windows 管理],表示系統根據一些因素來規範應用程式的背景活動。Background activity can be set to "Always allowed," "Never allowed," or "Managed by Windows," which means the app's background activity is regulated by the system according to a number of factors.

背景工作權限選項按鈕

傳統型的背景權限Background permissions on desktop

在傳統型裝置上,[由 Windows 管理] 設定會呈現為切換開關,而且預設會設定為開啟On desktop devices, the "Managed by Windows" setting is presented as a toggle switch, set to On by default. 如果使用者切換至關閉,則會出現一個核取方塊,可手動定義背景活動權限。If the user switches to Off, they are presented with a checkbox with which they can manually define the background activity permissions. 核取該方塊時,將允許應用程式隨時執行背景工作。When the box is checked, the app will be allowed to run background tasks at all times. 取消核取該方塊時,將會停用背景活動。When the box is unchecked, the background activity will be disabled.

開啟背景工作權限

關閉背景工作權限

在您的應用程式中,您可以使用 BackgroundExecutionManager.RequestAccessAsync() 方法呼叫傳回的 BackgroundAccessStatus 列舉值,以判斷其目前的背景活動權限設定。In your app, you can use the BackgroundAccessStatus enum value returned by a call to the BackgroundExecutionManager.RequestAccessAsync() method to determine its current background activity permission setting.

總括來說,如果您的 app 未實作負責任的背景活動管理,使用者可能會一併拒絕您 app 的背景權限,而這並非任一方樂意見到的。All this is to say that if your app doesn't implement responsible background activity management, the user may deny background permissions to your app altogether, which is not desirable for either party. 如果已拒絕您的應用程式在背景執行的權限,但需要背景活動以完成使用者的動作,則可以通知使用者,並將他們指向 [設定] 應用程式。If your app has been denied permission to run in the background but requires background activity to complete an action for the user, you can notify the user and point them to the Settings app. 這項作業可透過啟動設定應用程式,到 [背景應用程式] 或 [電池使用量詳細資料] 頁面來完成此操作。This can be accomplished by Launching the Settings App to the Background Apps or Battery Usage Details page.

使用省電模式功能Work with the Battery Saver feature

省電模式是使用者可在 [設定] 中設定的系統層級功能。Battery Saver is a system-level feature that users can configure in Settings. 它會在電池電量低於使用者定義的閾值時,截斷所有應用程式的所有背景活動,但已設定為 [一律允許] 的應用程式的背景活動除外It cuts off all background activity of all apps when the battery level drops below a user-defined threshold, except for the background activity of apps that have been set to "Always allowed."

藉由參考 PowerManager.EnergySaverStatus 屬性,從您的應用程式中檢查省電模式的狀態。Check the status of Battery Saver mode from within your app by referencing the PowerManager.EnergySaverStatus property. 這是列舉值:EnergySaverStatus.Disabled, EnergySaverStatus.OffEnergySaverStatus.OnIt is an enum value: either EnergySaverStatus.Disabled, EnergySaverStatus.Off or EnergySaverStatus.On. 如果您的應用程式需要背景活動,而且未設定為 [一律允許],然應該處理 EnergySaverStatus.On,方法是通知使用者,在省電模式關閉之前,不會執行指定的背景工作。If your app requires background activity and is not set to "Always allowed" then it should handle EnergySaverStatus.On by notifying the user that the given background task(s) will not run until Battery Saver is off. 儘管背景活動管理是省電模式功能的主要目的,您的 app 還是可以進行額外調整,進一步在省電模式開啟時節省能源。While background activity management is the primary purpose of the Battery Saver feature, your app can make additional adjustments to further conserve energy when Battery Saver is on. 在省電模式開啟的情況下,您的 app 可以減少其對於動畫的使用、停止位置輪詢,或延遲同步和備份。In the case where Battery Saver is on, your app could reduce its use of animations, stop location polling, or delay syncs and backups.

進一步最佳化背景工作Further optimize background tasks

以下是您可以在登錄背景工作時採取的額外步驟,以使其更省電。The following are additional steps you can take when registering your background tasks to make them more battery-aware.

使用維護觸發程序Use a maintenance trigger

不使用 SystemTrigger 物件,改用 MaintenanceTrigger 物件來判斷背景工作啟動時機。A MaintenanceTrigger object can be used instead of a SystemTrigger object to determine when a background task starts. 使用維護觸發程序的工作只有在裝置連接上 AC 電源時才能執行,並且能讓它們執行更長的時間。Tasks that use maintenance triggers will only run when the device is connected to AC power, and they are allowed to run for longer. 如需指示,請參閱使用維護觸發程序See Use a maintenance trigger for instructions.

使用 BackgroundWorkCostNotHigh 系統條件類型Use the BackgroundWorkCostNotHigh system condition type

必須符合系統條件,才能執行背景工作 (如需詳細資訊,請參閱設定執行背景工作的條件)。System conditions must be met in order for background tasks to run (see Set conditions for running a background task for more). 背景工作成本是一種度量單位,可代表執行背景工作的相對能源影響。The background work cost is a measurement that denotes the relative energy impact of running the background task. 將裝置接上 AC 電源時正在執行的工作會標示為 (對電腦有一些/沒有影響)。A task running when the device is plugged into AC power would be marked as low (little/no impact on battery). 當裝置是以電池電力執行且螢幕為關閉狀態時正在執行的工作會標示為,因為想必當時在裝置上執行的程式活動應該很少,所以背景工作會有更大的相對成本。A task running when the device is on battery power with the screen off is marked as high because there is presumably little program activity running on the device at the time, so the background task would have a greater relative cost. 當裝置是由電池供電,且螢幕為開啟狀態時,正在執行的工作會標示為,因為想必當時已經有一些程式活動正在執行,而背景工作會增加一些能源成本。A task running when the device is on battery power with the screen on is marked as medium, because there is presumably already some program activity running, and the background task would add a bit more to the energy cost. BackgroundWorkCostNotHigh 系統條件只會延遲您工作的執行功能,直到螢幕開啟或裝置已連接上 AC 電源為止。The BackgroundWorkCostNotHigh system condition simply delays your task's ability to run until either the screen is on or the device is connected to AC power.

測試電池效率Test battery efficiency

對於任何耗用高電量的案例,請務必在實際裝置上測試您的應用程式。Be sure to test your app on real devices for any high-power-consumption scenarios. 最好是在各種不同網路強度的環境中,在許多不同的裝置上,藉由開啟和關閉省電模式來測試您的 app。It's a good idea to test your app on many different devices, with Battery Saver on and off, and in environments of varying network strength.