This is the fifth in a series of blog posts describing how to get started with Windows Phone 7 development. It is written based on my own experience getting started with Windows Phone 7 development.
The previous entry, where I demonstrated how to use databound controls in xaml based webservice is available here.
The purpose of this post is to provide an overview of methods that will allow your application to seem to “run in the background” eventhough Windows Phone doesn’t actually allow it.
These methods are not hacks, they are well-documented and intended for the purpose that I describe here. I just couldn’t find a good overview for this purpose anywhere else, so I decided to try to summarize the documentation from msdn.
There are a number of techniques that can be used to make your application (seem to) do stuff, eventhough it is in the background:
- Tile schedule
- Push notifications
- Alarms and reminders
- Scheduled tasks
- Background audio
- Background file transfer
Tiles are the big squares that make up the start-page.
An application can have to kinds of tiles:
- Application tile
Created by user when he “pins” the application to the start screen.
Each application can have exactly 1 application tile on the start screen.
When the users taps the application tile, the application is launched to the normal startpage.
- Secondary tiles
Created in code from the application.
Each application can have any number of secondary tiles.
A secondary tile can act as a deep link into the application, so it can bypass the normal startpage.
If you set one or more of the properties on the back of the tile, Windows Phone will flip back and forth between the front and back of the tile with a nice animation, making your app look alive.
In project settings you can set the title and background image of your application tile. If you want to set the count or any of the back content, you can do this programmatically when the application runs or by editing /Properties/WMAppManifest.xml in your project.
All properties of secondary tiles are set programmatically.
Fore more information on tiles see Tiles for Windows Phone 7.
You can create a(ny number of) ShellTileSchedule that will update your application tile or any of your secondary tiles.
The schedule can run once, a number of times or indefinitely.
The schedule can run hourly, daily, weekly or monthly.
When you create the schedule, you supply a RemoteImageUri, from which the phone will attempt to fetch a new background image for the frontpage of the tile.
You can not change anything else on the tile.
For more information on tile schedules see How to: Schedule Tile Updates for Windows Phone
Push notifications agents
Push notifications provide a way for a third party developers to push messages to the phone.
Types of push notifications:
a toast message is shown on the top of the screen. If the user taps the toast, the application is launched.
Toast messages are not shown if the application is running.
Updates the application tile or any of the secondary tiles, with the supplied values. This could include (but is not limited to) a new count, or urls to new images.
Raw messages do not in themselves trigger any UI and are only delivered if the application is running.
Push notifications cannot start your application or trigger any processing except for download of images for the tiles.
For more information about push notifications see Push Notifications for Windows Phone
Alarms and reminders
By setting alarms and reminders, you can launch the phones default UI for alarm or reminder at a specified time (accuracy < 1 minute).
In short, alarms are meant to be loud and insisting (like an alarm clock) and reminders are more discreet (like the reminder for a calendar appointment).
For more information about alarms and reminders see Alarms and reminders overview for Windows Phone
Scheduled tasks associated to your application, allow you to execute code eventhough your application is not running. Scheduled tasks cannot have UI or launch applications, but may trigger tile update or toast notifications.
Scheduled tasks are restricted by requirements for memory usage, time until completion, available APIs, battery level, device limit on total allowed scheduled tasks etc.
If these restrictions are not met the task will not run or be terminated.
You have the choise between two types of tasks
- Periodic task
- Runs regularly (every 30 minutes +- 10 minutes)
- May only run for 25 seconds
- Ressource intensive task
- Runs when certain conditions are met: external power+high battery, wifi, screen lock, no call etc
- May run for 10 minutes
Tasks need to be rescheduled by the application every 2 weeks. They cannot reschedule themselves. If a task fails two consecutive times it will be be unscheduled.
The restrictions may cause a scheduled task to never be run.
For more information about scheduled tasks see Background agents for Windows Phone.
An AudioPlayerAgent is a specialized background agent, that allows you to control playback of local or streamed audio through the Zune Media Queue eventhough your application is not running.
The restrictions listed for scheduled tasks do not apply to AudioPlayerAgents.
For more information about background audio see Background Audio Overview for Windows Phone
Background file transfer
Background file transfer allows your application to queue a number of up- or downloads that will be executed in the background, regardless of wether the application is running in the foreground. The system manages the queue and decides wether it will process the queued items.
There are restrictions on the number of requests per application and per device as well as the size of the files.
Upload files must not exceed 5 MB.
Download file size limits range from 20 MB to unlimited. The limits are determined by the TransferPreferences property, which indicates the conditions that should be met, for the transfer to take place. The conditions are a combination of external power and/or wifi connection.
Background file transfers are automatically suspended and resumed based on wether the conditions are met.
For more information about background file transer see Background File Transfers for Windows Phone.