Skip to content

About

MAUI Background Services, Background Location Updates, Location Updates, BroadCastReceiver

Topics

Resources

Stars

73 stars

Watchers

9 watching

Forks

Repository files navigation

Background Location Service

LocationService running on Android

LocationService is a .NET MAUI sample (.NET 10) that keeps reporting the device location in the background on Android and iOS, and in the foreground on Windows and Mac Catalyst. Instead of polling on a timer, it listens to a stream of fixes, reports only the ones that matter, and drops to low power when the device stops moving.

How it works

  • When a position is reported: on a turn, after a set distance, when the device switches between moving and stationary, and as a heartbeat when nothing else happened.
  • Stationary mode: after 3 minutes within 50 m, GPS is switched off and fixes come at low power. Android wakes up through the significant-motion sensor, or a one-minute low-power poll on phones without one. iOS keeps low-accuracy updates running so the app is not suspended.
  • Bad fixes are filtered: impossible jumps are discarded, a coarse fix that still covers the known position does not replace it, and a single stray fix does not end stationary mode. On Android, a GPS fix that contradicts a fresh Wi-Fi fix is replaced by it.
  • Resuming: tracking resumes after a reboot on Android and after a background relaunch on iOS. When Android refuses to start the service because the app is not visible, it retries once the window is activated.

Project layout

Path Role
src/BackgroundServices/LocationUpdates.cs Shared worker. Filters fixes, applies the reporting rules and publishes LocationUpdate, LocationErrorMessage and ServiceMessage through IMessenger.
src/BackgroundServices/ReportingPolicy.cs Reporting rules, stationary detection and fix filtering, with no platform dependencies.
src/BackgroundServices/TrackingOptions.cs All thresholds and intervals.
src/Platforms/Android/Services/ AndroidLocationService (foreground service), AndroidLocationTracker (starts and stops it) and FusedLocationSource (Google Play fused provider).
src/Platforms/iOS/Services/iOsLocationSource.cs CLLocationManager with background updates enabled.
src/BackgroundServices/GeolocationSource.cs MAUI IGeolocation source for Windows, Mac Catalyst and Android devices without Google Play Services.
src/Utils/PermissionService.cs Location and notification permission requests.

Permissions

These are already set in the project; they are listed for reuse in your own app.

  • Android (AndroidManifest.xml): ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, ACCESS_BACKGROUND_LOCATION, FOREGROUND_SERVICE, FOREGROUND_SERVICE_LOCATION, POST_NOTIFICATIONS and RECEIVE_BOOT_COMPLETED. "While in use" location is enough to start; "Allow all the time" is asked once and is only needed to resume after a reboot. Some manufacturers still stop background work unless the app is excluded from battery optimization.
  • iOS (Info.plist): NSLocationWhenInUseUsageDescription, NSLocationAlwaysAndWhenInUseUsageDescription, NSLocationAlwaysUsageDescription, and location in UIBackgroundModes.

Configuration

Defaults in TrackingOptions, in meters, meters per second and degrees:

Option Default Meaning
FixInterval 5 s Fix rate while moving
TurnAngle, MinTurnSpeed 30°, 2 m/s Heading change that triggers a report, measured above walking pace
Distance 300 m Distance that triggers a report
MovingInterval 60 s Heartbeat while moving
StationaryRadius, StationaryDelay 50 m, 3 min What counts as stationary
StationaryInterval 10 min Heartbeat while stationary
StationaryPollInterval 1 min Stationary fix rate on phones without a motion sensor
MaxSpeed 70 m/s Faster implied movement marks a fix as bad
ReliableAccuracy 100 m Worst Wi-Fi accuracy trusted to correct a GPS fix

Build and test

Requires the .NET 10 SDK with the MAUI workload (dotnet workload install maui).

dotnet build
dotnet test tests/LocationService.Tests

The tests cover the reporting rules, the worker, the MAUI geolocation source and the view model, without a device. Background behaviour needs a real device; simulators do not reproduce it reliably.

License

Apache 2.0, see LICENSE.

About

MAUI Background Services, Background Location Updates, Location Updates, BroadCastReceiver

Topics

Resources

Stars

73 stars

Watchers

9 watching

Forks

Releases

Packages

Contributors

Languages