Battery awareness and background activity

Discussion in 'Live RSS Feeds' started by News, Aug 2, 2016.

  1. News

    News Extraordinary Robot
    News Feed

    Joined:
    Jun 27, 2006
    Messages:
    26,189
    Likes Received:
    20
    The Windows platform targets all kinds of devices: desktops, laptops, tablets, phones, HoloLens, IoT and Xbox. As a developer you want your users to have a great user experience with your app across the entire breadth of devices it is available on. Each of these devices has unique capabilities that your app should be aware of. Some have large screens, some have small screens. Some have keyboards for input, some have touchscreens. Some are stationary and plugged into a wall for power, while some are portable and use a battery.

    For portable devices specifically, all-day battery life is crucial in order for your app to be used to its fullest potential, and it is now an expectation of the modern mobile user. Battery-saving features will ensure your app is providing the best possible experience for users.

    Battery Use Settings

    How users perceive battery life on Windows is changing because it’s more visible to them than ever. Tools available to any average user in Settings show how much each app is impacting their device’s battery life. The Battery Use view breaks down the energy usage of your app while it is in use in the foreground of a user’s device or running quietly in the background, then ranks your app compared to the other apps on the device. This measured energy usage list is limited to the amount of energy your app uses on battery power and not while the device is plugged in and charging.

    [​IMG]

    Along with visibility into your app’s battery usage in the background, users are given new options in the Windows 10 Anniversary Update for controlling an app’s background activity.

    [​IMG]

    Users have three options available for each app’s background activity: Always Allowed in the Background, Managed by Windows and Never Allowed in the Background. These options allow a user to choose which apps’ background activities they consider important and which should be disabled.

    Background Access Status


    As a developer you have the ability to see which setting is currently applied to your app using the BackgroundAccessStatus enum. The first step in running code in the background is to request background access using BackgroundExecutionManager.RequestAccessAsync(). This returns the BackgroundAccessStatus enum value that details your app’s current ability to run in the background. With the new updates to the Settings available to users, this enum has also changed. There are now four new options:

    • AlwaysAllowed: The user has allowed your app’s background activity to always be able to run.
    • AllowedSubjectToSystemPolicy: Your app’s background activity is managed by Windows and currently is able to run.
    • DeniedSubjectToSystemPolicy: Your app’s background activity is managed by Windows and currently is not able to run.
    • DeniedByUser: The user has denied your app’s ability to run in the background.

    These new options are provided on devices with the Windows 10 Anniversary Update. On devices with previous builds of Windows 10 you could receive the previous values defined on MSDN. Below is an example of how to check for both sets of values:



    private async Task<String> CheckBackgroundActivitySetting()
    {
    var accessStatus = await BackgroundExecutionManager.RequestAccessAsync();
    string accessStatusString = "Unspecified";

    // Use API Information check to determine if Battery Use Settings values are available on this device
    bool isBatteryUseSettingAvailable = Windows.Foundation.Metadata.ApiInformation.IsEnumNamedValuePresent("Windows.ApplicationModel.Background.BackgroundAccessStatus", "AlwaysAllowed");

    if(!isBatteryUseSettingAvailable)
    {
    // Subset of values available on Windows 10 devices
    switch (accessStatus)
    {
    case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
    accessStatusString = "Allowed";
    break;
    case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
    accessStatusString = "Allowed";
    break;
    case BackgroundAccessStatus.Denied:
    accessStatusString = "Denied";
    break;
    case BackgroundAccessStatus.Unspecified:
    accessStatusString = "Unspecified";
    break;
    }
    }
    else
    {
    // Subset of values available on Windows 10 Anniversary Update devices
    switch (accessStatus)
    {
    case BackgroundAccessStatus.AlwaysAllowed:
    accessStatusString = "Allowed";
    break;
    case BackgroundAccessStatus.AllowedSubjectToSystemPolicy:
    accessStatusString = "Allowed subject to system policy";
    break;
    case BackgroundAccessStatus.DeniedBySystemPolicy:
    accessStatusString = "Denied due to system policy";
    break;
    case BackgroundAccessStatus.DeniedByUser:
    accessStatusString = "Denied";
    break;
    }
    }
    return accessStatusString;
    }


    Battery Saver


    Battery Saver is another tool users have to get the most out of their battery. Users can enable this feature to start automatically when the battery reaches a certain level in the Settings. Like the Battery Use options, Battery Saver manages all types of background activity in order to provide the best foreground experience for the user. When Battery Saver is on all forms of background triggers and extended execution are canceled or revoked. This reduced background activity allows the user to stretch out the last percentages of their battery until they can reach another power source to recharge. As a developer, you can determine its status using the PowerManager.PowerSavingMode property.

    If your app is a communication app and requires background activity such as push notifications to function, then you should warn your user that during battery saver mode that they will not receive notifications. Similarly, background transfers will be temporarily stopped while Power Saving Mode is on, so if a user initiates a download while Battery Saver is active you should notify your user that moving away from the app will halt the download.

    Best practices


    Here are some best practices for effectively performing background activity to ensure good battery life for your users:

    • Test your app in multiple power scenarios, just like you would for multiple screen sizes. If your app is going to run on devices that are plugged into the wall for power as well as on devices that have a battery, test using both of those configurations. Make sure to test your app under battery saver.
    • Move any opportunistic background work to the MaintenanceTrigger. The Maintenance Trigger provides a much longer time for background activity to complete, around ten minutes, and runs only when the device is connected to AC power. This way you can complete your background work in a single shot rather than waking up the system frequently to complete small parts of the work.
    • Use the BackgroundWorkCostNotHigh System Condition when registering your opportunistic background tasks. Background work cost is the cost on the device to do background work. A Low cost work item would run when the device is plugged into AC Power, a Medium cost work item would run when on battery power and the screen is on for active usage, and a High cost work item would run when the device is on battery power and the screen is off in an attempt to reduce power usage. This system condition ensures that your background work will run when resource usage is in the Low or Medium categories. Below is an example of how conditions are added to background task registration.



    private void RegisterBackgroundTask()
    {
    var builder = new BackgroundTaskBuilder();
    builder.Name = "My Background Trigger";
    builder.SetTrigger(new TimeTrigger(15, true));
    builder.AddCondition(new SystemCondition(SystemConditionType.BackgroundWorkCostNotHigh));
    BackgroundTaskRegistration task = builder.Register();
    }


    • All apps start with their background activity set to Managed by Windows. This allows the system to determine the most effective balance of background activity to battery savings depending on how the user interacts with their apps.

    Wrapping Up

    Battery usage enables portability of your experience. Knowing how to scale your app’s experience based on battery availability will allow your users to enjoy your app for more time, in more environments, and on more devices. Users have new visibility and choice for enabling battery draining background activity per app. Be mindful of your battery use in the background, as users may choose to disable background activity for your app or uninstall it completely. Using the best practices described above will enable a great experience for your users across all of their portable devices.

    Get started with Windows App Studio.

    Continue reading...
     

Share This Page

Loading...