Background Location Tracking
In Windows Phone 7.5, apps that made use of
location tracking were subject to events being lost when deactivated.
When the app was dormant or tombstoned, PositionChanged
event handlers would be discarded—lost forever, leaving the app without
any knowledge of changes in location for the period before reactivation.
This has changed in Windows Phone 8, which
allows only a single app to be in execution at any one time unless that
app is a location tracking app, in which case it can continue to run in
the background while a different app is running in the foreground.
To nominate your app as a location tracking app, add a BackgroundExecution
element to the DefaultTask element in your project’s WMAppManifest.xml file, as shown:
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" >
<BackgroundExecution>
<ExecutionType Name="LocationTracking" />
</BackgroundExecution >
</DefaultTask>
With this in place, your app will continue to run as long as it remains subscribed to a Geolocator
object’s PositionChanged
event.
When your app is running in the background,
it is important to stop all tasks not related to location tracking. To
be notified when your app is running in the background, subscribe to
the RunningInBackground
event of the PhoneApplicationService
. This can be done in your project’s App.xaml file as shown:
<Application.ApplicationLifetimeObjects>
<shell:PhoneApplicationService
RunningInBackground="HandleRunningInBackground"
... />
</Application.ApplicationLifetimeObjects>
The event handler is placed in your App.xaml.cs file, like so:
void HandleRunningInBackground(object sender, RunningInBackgroundEventArgs e)
{
/* Stop all tasks not related to location tracking. */
}
The RunningInBackground
event enables your app to suspend unnecessary tasks, such as updating the UI.
Note
Only one location tracking app can be running
in the background at any time. The most recently opened location
tracking app takes precedence and causes any existing location tracking
apps to be made dormant or tombstoned.
Although location tracking apps are currently
the only type of app that can be enabled to run in the background, plan
to see other types of background apps in the future.
Testing Apps That Use the Geolocator
The Windows Phone emulator includes a tool
for simulating location tracking, which can be accessed by clicking the
Additional Tools button on the emulator menu , and then selecting the Location tab on the Additional Tools windows.
The location simulator works in two modes: a
live mode that allows you to interact with the map to send location
data to the emulator as soon as you click on the map and a playback
mode that causes the location simulator to step through a sequence of
map points, which you enter by clicking on the map (see Figure 2).
FIGURE 2. Location simulator.
Map points can be saved to a file and loaded again at a later time.
Note
The Play button is enabled only when the Live button is toggled off.
When using the location simulator, any Geolocator
instances in your app respond as though they are receiving actual
location data, giving you the opportunity to test your app’s geo
location features.