Android Geofence BroadcastReceiver not invoked after app is swiped away (killed state)


I have implemented Android Geofencing in a Flutter app using native Kotlin code.

The geofence works correctly when:

  • App is in foreground working

  • App is in background working

However, when I swipe the app away from Recents (killed state), I do not receive any geofence events.

GeofenceBroadcastReceiver.onReceive() is never called:

class GeofenceBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        LocalFileLogger.log(
            context,
            "GEOFENCE_RECEIVER",
            "BroadcastReceiver Invoked"
        )

        Log.e("GEOFENCE_KILLED", "Receiver Invoked")

        val event = GeofencingEvent.fromIntent(intent)

        if (event == null || event.hasError()) {
            return
        }

        when (event.geofenceTransition) {
            Geofence.GEOFENCE_TRANSITION_ENTER ->
                Log.e("GEOFENCE_KILLED", "ENTER")

            Geofence.GEOFENCE_TRANSITION_EXIT ->
                Log.e("GEOFENCE_KILLED", "EXIT")

            Geofence.GEOFENCE_TRANSITION_DWELL ->
                Log.e("GEOFENCE_KILLED", "DWELL")
        }
    }
}

Geofence registration:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(
        context,
        GeofenceBroadcastReceiver::class.java
    )

    val flags =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.FLAG_UPDATE_CURRENT or
                    PendingIntent.FLAG_MUTABLE
        } else {
            PendingIntent.FLAG_UPDATE_CURRENT or
                    PendingIntent.FLAG_IMMUTABLE
        }

    PendingIntent.getBroadcast(
        context,
        0,
        intent,
        flags
    )
}

geofencingClient.addGeofences(
    request,
    geofencePendingIntent
)

Manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

<receiver
    android:name=".GeofenceBroadcastReceiver"
    android:enabled="true"
    android:exported="false"/>

I also write logs to:

  • Logcat, SharedPreferences

  • Internal txt file (context.filesDir)

None of them are updated after the app is swiped away, which makes it appear that BroadcastReceiver is never invoked.

I have already verified:

  • addGeofences() succeeds

  • ACCESS_BACKGROUND_LOCATION is granted

  • Battery optimization is disabled

  • Geofence works in foreground and background

Question: Is it expected that geofence broadcasts stop after the app is removed from Recents on some Android devices? If so, what is the recommended approach to reliably handle geofence events and persist logs when the app process has been killed?

0
Jul 3 at 5:15 AM
User AvatarAravind Kumar Korumilli
#android#logcat#android-location#geofencing#android-geofence

No answer found for this question yet.