May 24, 2011

One of most requested and discussed features of Mango release is “multitasking”. In Mango the multitasking term has slightly different meaning than standard (PC) multitasking. Multitasking for Mango phones means ability to execute the code while application in not active and play audio started by the application in a background. This is achieved by Background Agents. In addition to them Mango also introduces APIs to download and upload files while application is in the background and add Reminders and Alarm to grab user attention.

This post will show how to use those features. During the post I’ll show how to build sample application which uses those features. The application’s UI is very simple:

Let’s start from background agents. The Background Agent are class libraries which “manifested” with application and capable of performing periodic tasks (or resource intensive task). To help starting with agents creation Visual Studio provides number of templates:

Background agent is a part of the application and as such it can access all resources available to the application. In this sample the background agent will update file in application’s IsolatedStorage with current DateTime timestamp. Let’s see the implementation details:

The code is very simple. In order to make this class library as application’s background agent we need to add a reference to it from the main application. It will add a corresponding reference also in WMAppManifest.xml:

From now on the class library is ready to serve as our application’s background agent. After deploying the application to he Windows Phone Emulator (or device) and navigating to Settings/Applications/background services you will find that our application has background service:

and you can turn off background services to any of your applications.

Now let’s get back to the main app and wire the code for enabling/disabling running the background agent:

Clicking on reminder will navigate into the application and will use the provided URI (deep linking):

Note “REMINDED” added to the button caption when identified that application navigated from the reminder.

The Alarm is pretty similar to the Reminder; it will enable to use any application’s sound as alarm sound (or will use system alarm sound if not provided) but will not have an option to deep link into the application:

Last very exciting feature is a Background File Transfer. It enables to transfer files (both directions – upload and download) even when application is not active or tombstoned. Once application activated all the information from corresponding background transfers (events) will be delivered to the app. Let’s download big video file (~30Mb) using background transfer.

Big files will use WiFi by default to download , like marketplace applications does. Also in some cases big files will be downloaded only when device is connected to power source. In order to override this default behavior change the TransferPreferneces property of BackgroundTransferRequest instance.

The application limited to 5 requests in the BackgroundTransferService queue and need to manage their requests. If application adds more than 5 requests the BackgroundTransferService.Add() command will throw an exception.