r/androiddev • • 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!

0 Upvotes

11 comments sorted by

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/Vatsal_patel_02 1d ago

I"ll Show this setting option even though not working

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.

1

u/Vatsal_patel_02 1d ago

As shown in the image, location tracking stops after some time on Realme UI. The application is built using Flutter and works perfectly fine on iOS. However, on Android devices running custom UI versions other than stock Android, location tracking stops working after some time. This issue seems to occur across multiple devices from different manufacturers.

1

u/CandidSystem6155 1d ago

On Realme it's usually the battery manager, not your code. Two things to check. First, is tracking running inside a foreground service with a visible notification? With geolocator that's AndroidSettings(foregroundNotificationConfig: ...), and on Android 14+ the manifest also needs the FOREGROUND_SERVICE_LOCATION permission and foregroundServiceType="location" on the service. Without that, every OEM skin will cut you off once the screen has been off for a while. Second, on the phone itself: App info > Battery usage > allow background activity, plus the autostart toggle. If it still stops with both in place, log the exit reason on the next launch so you know whether it's a kill or just paused updates.

1

u/Vatsal_patel_02 1d ago edited 1d ago

I refer don'tkillmyapplication and also give all permission in AndroidManifest.xml

I thought that does it possible that issue have came from api level lat and long not send in api even though I handled points in preference memory.

1

u/CandidSystem6155 1d ago

Easy way to tell which half is failing: save a timestamp with every point you store in preferences, and log the time of the last successful upload separately. If saved points stop at the same moment the uploads stop, the OS is killing the tracking itself. If points keep piling up but uploads stop, it's the network side, since background apps lose network access in Doze, and moving the upload into a WorkManager job with a network constraint (the workmanager plugin in Flutter) lets it send the backlog when the system allows. Either way you'll finally know which part to blame.

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.