Windows Phone 8.1 Battery Saver, Background Agents, and Background Tasks
I worked a support case recently where the customer had a Windows Phone Silverlight 8.0 background agent running on Windows Phone 8.1. My customer noticed that the background agents would not run when battery saver was enabled. In order to explain why this was happening, I first need to tell you some of the history behind Battery Saver:
Battery Saver was introduced in Windows Phone 7 to allow the OS to turn off battery-consuming operations such as background agents in order to conserve energy. In it's original configuration, it was very simple. You could elect to have it turn on when the battery was low or you could turn it on until it charges up again. In addition, it explicitly says "apps won't run in the background", with certain exceptions like email and text messaging.
In Windows Phone 8.0, we upgraded the experience so that you could turn it on all of the time: apps would never run in the background. The option to turn it on only when battery gets low is still there, as well as the option to turn it on until next charge is still available.
WP8 Battery Saver Main Screen
|WP8 Battery Saver Advanced Screen|
Fast forward to Windows Phone 8.1. We realized that Battery Saver might not be easily found, so we moved it to the Start Menu. We also realized that some people might want certain apps running all of the time, regardless of the battery state, but still wanted the value that Battery Saver provides. So now we allow you to exempt apps from Battery Saver, which means that they can run regardless of whether Battery Saver is running or not.
WP8.1 Battery Saver Main Screen
WP8.1 Battery Saver Usage Screen
WP8.1 Battery Saver App Screen
:Footnote: Windows Phone 8.1 GDR1 (not shown) clarifies that "low" means 20% of battery life.
Windows Phone Background Agent vs Tasks Starting with Windows Phone 7, we gave developers the option to allow apps to run silently via Background Agents using the Silverlight APIs. These Agents can run on a periodic schedule with certain limitations. However, we are clear in the documentation about Battery Saver and Background Task Periodic Agents:
|Battery Saver mode can prevent execution||Battery Saver mode is an option that the user can enable on the device to indicate that battery life should be prioritized. If this mode is enabled, periodic agents may not run, even if the interval has elapsed.|
What we must keep in mind is that this was written in the Windows Phone 8 timeframe, before WinRT APIs were available for Phone.
With the release of Windows Phone 8.1, we got two new features in this area:
2) Ability to allow apps to run under Battery Saver by checking “Allow this app to run in the background even when Battery Saver is on”.
What’s important to note is that Windows Phone Silverlight Background Agents are not the same as WinRT Background Tasks, even though they have the same purpose.
My customer was running a Windows Phone Silverlight Background Agent, and expected that the setting in the Windows Phone 8.1 Battery Saver would allow the Background Agent to execute regardless of the Battery Saver status. What he noticed was that it would not executed at the specified interval whenever Battery Saver was functioning. My customer opened a support case with my team to diagnose the problem.
We discovered that Silverlight Background Agents do not have the same runtime check for Battery Saver exemption status as WinRT Background Tasks, and therefore they do not respect that setting.
It’s important to note that there is a bug, but not the bug that you might think. We should make it clear that Silverlight apps do not respect this setting in our documentation, and we should also make it clear in the Battery Saver itself.
Windows Phone Silverlight Background Agents will not run while Battery Saver is active on the phone, regardless of whether or not the “Allow this app to run in the background even when Battery Saver is on” setting is checked. This setting is reserved for Windows Phone 8.1 WinRT apps exclusively.
Comments are encouraged, and feel free to tweet to me at @WinDevMatt. My team’s hashtag is #WSDevSol.