Background Recorder with Foreground Service (Android only)
This example demonstrates how to build a Flutter app using the Maps SDK to record a user’s track while running in the background.
The app integrates with the Android foreground service and notifications API to keep recording active even when the app is not visible.
How it works
The example app highlights the following features:
- Requesting location and notification permissions.
- Initializing a foreground service to enable background location updates.
- Recording a track and saving it to device storage.
- Displaying the recorded track and duration on the map.
UI and Map Integration
The following code builds the UI with a GemMap widget and app bar controls for recording and following the user’s position.
Foreground Service Setup
The AndroidForegroundService class wraps the flutter_background_service and flutter_local_notifications plugins. It initializes the notification channel, requests permissions, and manages the lifecycle of the foreground service.
Requesting Permissions
The app requests both notification and background location permissions before enabling recording and the foreground service.
Starting and Stopping Recording
Recording only starts after the foreground service is initialized. When stopped, the service is shut down and the recorded track is presented.
Presenting the Recorded Track
When recording is complete, the last session is loaded from disk and displayed on the map.
Utility Functions
- Android
Add the following code to the android/app/src/main/AndroidManifest.xml file, within the <manifest> block:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />This example uses the Permission Handler package. Be sure to follow the setup guide.


