mobile · 2 min read

Android Foreground Service Killed on Android 12+ — Location Tracking Fix

Our delivery rider location tracking stopped after 5 minutes on Android 12+. Foreground service type declaration was the missing piece.

Vikash Kumar

Vikash Kumar

· 2 min read

Problem

Background location tracking stops after 5 minutes on Android 12+

Solution

Declare foregroundServiceType=location in manifest, request permissions in correct order, restart service on app resume

FlutterAndroidGeolocatorForeground Service

Problem

Delivery riders using our Flutter app reported that live location tracking stopped updating after about 5 minutes with the app in background. Dispatch could no longer see rider positions. The issue spiked after users upgraded to Android 12 and 13.

Environment

  • Flutter with android foreground service via flutter_foreground_task
  • geolocator for position stream
  • Android 12–14, targetSdk 33 at time of bug

Root Cause

Android 12 requires declaring foregroundServiceType in AndroidManifest.xml. We had a foreground notification but no location type declaration. Android killed the service silently when the app went to background because the system treated it as an invalid foreground service.

Solution

AndroidManifest.xml:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<service
  android:name=".LocationForegroundService"
  android:foregroundServiceType="location"
  android:exported="false" />

Flutter side — request background location only after foreground is granted, and start the service with explicit type on Android 14:

await FlutterForegroundTask.startService(
  notificationTitle: 'Tracking delivery',
  notificationText: 'Location active',
  callback: _locationCallback,
);

// Android 14+: verify service is running after resume
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  if (state == AppLifecycleState.resumed) {
    _ensureTrackingServiceAlive();
  }
}

Prevention

  • Audit manifest foregroundServiceType for every background feature
  • Test background location for 30+ minutes on Android 12+ physical devices
  • Log service start/stop events to Crashlytics breadcrumbs
  • Show riders a clear persistent notification — required and builds trust

Result

Location update gaps dropped from 23% of shifts to under 2%. Dispatch SLA for rider visibility went back to 99.5%.