You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -15,8 +15,6 @@ The power management APIs use a callback-based model similar to the existing [Po
15
15
16
16
## Prerequisites
17
17
18
-
19
-
20
18
To use the app lifecycle API in the Windows App SDK:
21
19
22
20
1. Download and install the latest release of the Windows App SDK. For more information, see [Install tools for the Windows App SDK](../set-up-your-development-environment.md).
@@ -26,6 +24,8 @@ To use the app lifecycle API in the Windows App SDK:
26
24
27
25
The following example demonstrates how to subscribe and respond to [PowerManager](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager) events. This code subscribes to the [BatteryStatusChanged](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager.batterystatuschanged) event during startup. The app then responds to changes by checking the current power level and adjusting its resource usage appropriately. For example, if the battery discharges at a low power state, the app might defer any non-critical background work.
28
26
27
+
### [C++ sample](#tab/cpp)
28
+
29
29
> [!NOTE]
30
30
> Apps can register and unregister for these events at any time, but most apps will want to set callbacks in `WinMain` that persist as long as the app continues to run.
> Apps can register and unregister for these events at any time, but most apps will want to set callbacks that persist as long as the app continues to run.
OutputFormattedMessage($"Battery status changed: {status}, {remainingCharge}% remaining");
144
+
DetermineWorkloads();
145
+
}
146
+
147
+
privatevoidOnPowerSupplyStatusChanged()
148
+
{
149
+
//...etc
150
+
}
151
+
```
152
+
153
+
---
154
+
99
155
## Configure app logic based on multiple status values
100
156
101
157
[PowerManager](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager) events are relatively low-level, and in some scenarios, a single event handler being called might not provide enough information for the app to decide how to behave. In this example, the [PowerSupplyStatusChanged](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager.powersupplystatuschanged) event could be called when the device is disconnected from power. In that case, the app must check the current battery status before deciding how to proceed.
The [PowerManager](/windows/windows-app-sdk/api/winrt/microsoft.windows.system.power.powermanager) class offers information about other device states relevant to an app's power usage. For example, apps can disable graphics processing when the device's display is turned off.
OutputFormattedMessage($"Display status changed: {status}");
275
+
if (displayStatus==DisplayStatus.Off)
276
+
{
277
+
// The screen is off, let's stop rendering foreground graphics,
278
+
// and instead kick off some background work now.
279
+
StopUpdatingGraphics();
280
+
StartDoingBackgroundWork();
281
+
}
282
+
}
283
+
```
284
+
285
+
---
286
+
168
287
## Unsubscribe from events
169
288
170
289
Apps can register and deregister for notifications during their lifecycle. Use your language's preferred event registration management system if your app doesn't need to receive power status notifications during its entire lifecycle.
0 commit comments