Windows Phone 7 Background File Transfer

Mobile applications by their nature need to be able to work disconnected from their central data source. If you force users to always have an internet connection to use your app, you’re doing it wrong. For Inspire this is especially true since our users do amazing things like help victims after earthquakes or clean up the mess hurricanes leave behind, and they can’t rely on the internet always being there.

The downside to working offline, is downloading all of a users information to their device can take a long time. There are many strategies to deal with this issue, and the one we chose to implement is supported natively on the Windows Mobile platform – Background File Transfer. Basically it’s an API for you to make asynchronous HTTP(S) GET and POST requests.

We’re using it to asynchronously fetch our users contact information, groups, tags, etc. This can be a lot of data but since we’re backgrounding the download, we can allow users to interact with their contacts, meanwhile updating information from the server in the background.

Microsoft has a helpful “getting started” article that we followed when initially spiking on this. It’ll give you the gist of what it takes to interact with the API.

The article can get a little complicated because it’s trying to do too much. To clear that up a bit you really only need to focus on the code that does this:

// This happens to be a .json file
string transferFileName = "";
Uri transferUri = new Uri(Uri.EscapeUriString(transferFileName), UriKind.RelativeOrAbsolute);
BackgroundTransferRequest request = new BackgroundTransferRequest(transferUri);
request.Method = "GET";

// Decide where you'll download the file to
string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1);
Uri downloadUri = new Uri("shared/transfers/" + downloadFile, UriKind.RelativeOrAbsolute);
request.DownloadLocation = downloadUri;

// set preferences about when the transfer should perform the transfer
request.TransferPreferences = TransferPreferences.AllowCellularAndBattery;

     // This queues your background request.
} catch(Exception e) {
     // This could fail so you'll want to handle exceptions

After that you’ll have a request waiting to be processed.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s