It's common on mobile devices for the operating system to suspend the execution of apps when they are not visible to the user (in the background, for example). Suspending such apps helps preserve resources and provides better security. For an app that relies on using the device location, be aware that background apps won't receive location updates from the system without additional configuration by the developer. If your app needs location updates while running in the background, you can configure it so the system will not suspend your app while location services are active.
When to use background location tracking
For most mobile apps, location updates are only needed when the app is in the foreground. For example, a navigation app that provides turn-by-turn directions only needs to know the user's location when the app is open. In this case, you can use the default behavior of the operating system to suspend the app when it is not visible to the user.
However, some scenarios require location tracking even when the app is in the background.
For example, apps may need constant location updates when doing things like:
- Recording the user's path while hiking, running, or cycling
- Tracking a mobile worker's location relative to a geofence
- Providing location-based notifications or alerts
Configure background location tracking
Android and iOS use different mechanisms to allow apps to receive location updates when they are not visible to the user. For each platform, you need to configure additional permissions for your Flutter app.
Android requires the use of a foreground service to track location in the background. Foreground services permit your app to asynchronously perform operations that are noticeable to the user (a status bar notification let users know that your app is executing an operation and consuming system resources).
The following prerequisites must be satisfied before you are able to receive location events in the background:
-
Ensure
SystemLocationDataSource.backgroundUpdatesEnabledis set totrue. -
Add the following location permissions to the
Android:Manifest.xml android/app/src/main/AndroidManifest.xmlUse dark colors for code blocks 1 2 3 4 5 6 7 8 9 10Add line. <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:label="flutter_project_template" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <!-- ... -->To learn more about requesting user authorization, see the Note in the Location data sources section of the Device location guide topic.