Gaining Access: Making Sure Your Windows 8 App Background Task Has What It Needs

Overview

In Windows 8, Windows Store (formally known as metro style) apps have the ability to define background tasks that can perform tasks that run even when the app isn’t in the background. Though to make sure the user’s battery doesn’t cry uncle prematurely these background tasks are resource constrained. In this post we’ll take a look at how Windows 8 does resource management for background tasks.

The Constraints

Windows 8 performs resource management for background tasks through the use of resource constraints. There are two main types of constraints a background task needs to worry about: CPU time and network usage. The amount of CPU time and network usage an app’s background task is allocated depends on if the app is on the lock screen or not.

A Windows Store app is considered a lock screen app if it occupies one of the seven available lock screen slots on the lock screen.

When your app is a lock screen app Windows will give your background tasks a higher quality of service over resources then a non-lock screen app. The CPU time constraint is enforced regardless of the current power state of the device but the network usage constraints only come into play when PC off of AC power.

 

Note: Windows will dynamically adjust the network usage constraint based on the power usage of the active network link. Typically, Wi-Fi connections use less power than the cellular connections and will be allowed to send more over the link over a given period of time than cellular connections.

The Elite

Among all the possible triggers available to an app to trigger their background tasks the following triggers are considered “critical” as far a Windows is concerned.

  • Push notification trigger
  • Control channel trigger

These triggers are meant to be used during real time scenarios like receiving a VOIP call. These background tasks are given guaranteed app resource quotas according to the resource quotas listed previously. Meaning Windows will not try and terminate these tasks early even if the system becomes resource constrained. These triggers require an app to be a lock screen app in order to be used.

The Hook

If your app doesn’t require the increased resources or trigger capabilities of a lock screen app you’re better off sticking with triggers that don’t require lock screen capabilities. It’ll simplify things but from a programming and certification standpoint and from a user standpoint in terms of battery life.

If you do require lock screen capabilities for your background tasks here’s what you need to know. To be on the lock screen you’ll need to provide an additional image asset, a 24 by 24 pixel png image, for the badge that gets displayed on the lock screen. As there are only 7 slots available on the lock screen for apps you’ll be competing with other apps for those slots so you’ll have to make a compelling case to the user to add your app. Which brings us to our next point, the app will needs to be added to the lock screen explicitly by the user.

That last point is key, there is no programmatic way to add an app the lock screen. Windows does toss the app a bone by giving a way for the app to programmatically ASK the user to add the app to the lock screen though the BackgroundExecutionManager.RequestAccessAsync() method but it comes with a big caveat. It’s a one shot deal. No matter the outcome: yes, no or dismissed it will only work once.

Making the Connection: Lighting up Metro IE’s Ability Direct Link to an App in the Windows Store

How do I make users aware that my app exists? That’s most likely one of the most prominent questions a developer’s ask themselves when they release an app, and for good reason. No one likes to see their hard work go to waste. A little known feature in Metro IE is its ability to advertise the availability of Windows Store app (also known as a Metro style app) for the current website in the Windows Store though Metro IE’s appbar.

Metro IE allows a website to specify the following actions for an app:

  • Install an app if it’s not already installed
  • Switch to an app if it’s already installed

A website can choose to have just one of the options active or both depending on their needs. The way Metro IE recognizes a website has an associated Windows Store app is through the use custom meta tags in the header area of a website’s page.

The minimum required meta tags to light this up are:

  • msApplication-ID
  • msApplication-PackageFamilyName

Both of these can be found in the app manifest of the project once the app is associated with store though Visual Studio and are used to link the website and Windows Store app together.

When an app is launched though Metro IE in this manner a custom argument can be specified through another meta tag with the name “msApplication-Arguments”. The app can then read this argument from the OnLaunched method in App.xaml.cs.

As actions speak louder than words, if you want to see this behavior in action you just visit Relay’s website in Metro IE and give it a try. For context, Relay is our sample notification app, feel free to give that a try too while you’re at it :)

The Bing homepage also has this integration setup if you want to see another live example.

Until next time!