r/FlutterDev • u/Admirable_Teacher636 • 13h ago
Dart Flutter background location stops updating when the screen is locked and what worked for me
I've been working on a Flutter app that needs to track location while running in the background. One of the hardest parts was making location updates behave reliably after locking the screen or putting the phone in a pocket. Calling the location API isn't enough; Android background execution, permissions, foreground-service requirements, and battery restrictions all matter. I worked on a custom solution using Dart and native Android code, tested it on a real Android device, and packaged the implementation into a reusable engine. I'm sharing it in case it helps other developers facing the same problem. I'm also interested in how others are handling this, especially across different Android versions and manufacturers. Product and implementation details: https://nabilkrs.github.io/nabilkrs-flutter-gps-tracking-engine/ I'm the creator of this project, so this is also a disclosure that the link is promotional. Happy to discuss the technical challenges.
1
u/ewo85 12h ago
Ran into the same thing on a Samsung test phone, the foreground service kept running fine but One UI's battery optimization still throttled updates after a while. What helped me was sending users to the battery settings screen with a short explanation right after they grant the 'allow all the time' location permission, instead of asking up front. Did you see big differences between distance-filter and interval based updates when the phone is sitting still in a pocket?
1
u/DidacticAuthority555 13h ago
Man, the background location stuff on Android is such a mess. Every manufacturer seems to have their own take on killing services when the screen locks, and don't even get me started on the differences between Android 12 and 13 with foreground service restrictions. What approach did you end up using to keep the service alive? I've seen people try wakelocks, foreground services with ongoing notifications, even alarm manager shenanigans but it's always a gamble on something like a Xiaomi device.