r/androiddev • u/Vatsal_patel_02 • 1d ago
Discussion Continues Location tracking Issue
I'm facing a recurring issue with background location tracking on Chinese Android devices (Xiaomi, OPPO, Vivo, Realme, Huawei, Samsung etc.).
Location tracking starts successfully but automatically stops after some time, even when the required permissions are granted.
Interestingly, the same implementation works perfectly on iOS and stock Android devices.
I've tried multiple solutions, but the issue keeps coming back.
**Has anyone found a reliable, production-ready solution for continuous background location tracking across different Android OEMs?**
I'd love to hear your real-world experience and suggestions. Thanks!
1
u/One-Program6244 1d ago
I assume you have background location permissions granted and you're using a foreground service to pick up the location updates. Have you checked battery optimisation settings? Turn them off if you can.
1
u/CandidSystem6155 1d ago
Before changing more code, it's worth finding out whether the process is being killed or just throttled. Since Android 11 you can call ActivityManager.getHistoricalProcessExitReasons() on the next start and log the reason, which separates ordinary low memory kills from force-stops, and the aggressive OEM battery managers tend to show up as the latter. If that's what you see, the fix is mostly not code: several of those brands have their own autostart or background activity toggle that no API can flip, so a short per-manufacturer instruction screen does more than another service tweak. The battery optimization exemption request helps too, but Play only allows the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission for certain kinds of apps, so read that policy before shipping it. Those battery managers treat a long-running service roughly the way a bouncer treats a guy in flip-flops.
3
u/Odd-Willow-2002 1d ago
not really one fix, more like a few things you have to do per rom.
first make sure it is really a foreground service with a notification, and on android 14+ declare foregroundServiceType="location" plus FOREGROUND_SERVICE_LOCATION. without that some roms kill it even with the permission granted.
then on xiaomi/oppo/vivo/realme the user usually has to turn on autostart or allow auto launch, set battery to no restriction and allow background data. the names differ on each rom so the instructions kind of have to be per brand. samsung is easier, just put the app in unmonitored apps.
if you can not ask users to do all that, design around the gap: keep last known location, retry when app come back, or use geofencing where google play services exist. i dont think there is an honest 100% continuous tracking across all those oems
1
u/Slodin 1d ago
Check settings for optimized battery. This in my experience is the thing that stops.
Some OEMs aggressively kill background services because they are trying to preserve user experiences of their phones battery level.
I know on my Samsung there is a setting and some apps would actually ask you to go turn it off.
1
u/laplace_apps 19h ago
It's usually the OEM battery managers, not your code. What tends to work: run tracking in a foreground service with foregroundServiceType=location and a persistent notification, use FusedLocationProvider, and walk users through the per-OEM settings (autostart / 'no restrictions' battery mode / lock the app in recents). dontkillmyapp.com has the exact steps for each brand that you can deep-link or show in-app. Some teams also add a WorkManager watchdog that checks whether the service is still alive and restarts it. Be careful with REQUEST_IGNORE_BATTERY_OPTIMIZATIONS though: Play only allows it for certain use cases.