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.
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.
3.134.78.137