Freigeben über


Running location-tracking apps in the background for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

On Windows Phone, only one app runs in the foreground at a time. If the user navigates away from an app by pressing the Start button or by launching another app, the current app is suspended. If the new foreground app requires more memory than the suspended app, the suspended app may be terminated and tombstoned. For more information on the life cycle of Windows Phone apps, see App activation and deactivation for Windows Phone 8. On Windows Phone 8, a location-tracking app can continue to run in the background after the user navigates away, as long as the app continues to actively track location. This feature enables scenarios such as an app that provides turn-by-turn directions. For a walkthrough of creating a simple app that runs in the background, see How to run location-tracking apps in the background for Windows Phone 8.

Note

Apps that track location in the background automatically participate in Fast Resume. For more info on this feature, see Fast app resume for Windows Phone 8.

Deactivation of apps running in the background

This section lists the conditions under which the operating system will deactivate an app running in the background. When an app running in the background is deactivated, the Deactivated event is raised just like a regular foreground application. Your app can check to see the reason for deactivation in the Deactivated event handler by checking the Reason property.

  • The app stops actively tracking location. An app stops tracking location by removing event handlers for the PositionChanged and StatusChanged events of the Geolocator class or by calling the Stop()()() method of the GeoCoordinateWatcher class.

  • The app has run in the background for 4 hours without user interaction.

  • Battery Saver is active.

  • Device memory is low.

  • The user disables Location Services on the phone.

  • Another app begins running in the background.

It is a good practice to test your app under these different conditions, such as with Battery Saver turned on or with Location Services disabled.

API restrictions while running in the background

There is a limited set of APIs that you can use while your app is running in the background. For a list of the APIs that are allowed, see Features that can be used while running in the background for Windows Phone 8.

Best practices for apps running in the background

For apps running on mobile devices, power consumption is an important consideration. While your app is running in the background, there are steps you can take to reduce battery usage and therefore improve user satisfaction with your app. The example described in How to run location-tracking apps in the background for Windows Phone 8 shows you a simple way to turn off unnecessary features when your app moves to the background.

  • Minimize the number and frequency of network requests. When possible, you should group your requests and perform them in batches at intervals of one minute or more instead of performing single requests every few seconds.

  • Stop active Timer and DispatcherTimer objects if they are only used for foreground functionality such as UI.

  • Stop all XAML animations.

  • If your application no longer needs to run in the background, stop tracking location by removing event handlers for the PositionChanged and StatusChanged events of the Geolocator class or by calling the Stop()()() method of the GeoCoordinateWatcher class.

See Also

Other Resources

How to run location-tracking apps in the background for Windows Phone 8

Features that can be used while running in the background for Windows Phone 8