Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor geofences in background on Oreo?

I followed this tutorial: https://developer.android.com/training/location/geofencing and works fine on Android < 8, but in Oreo i have problems due to new OS background limitations.

How can I get geofence transition triggers when app is in background?

I also tried to use a BroadcastReceiver instead of IntentService, but the result is the same.

Pending Intent:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
    intent.action = "com.example.GEOFENCE_TRANSITION"
    PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Register geofence:

geofencingClient.addGeofences(request, geofencePendingIntent).run {
    addOnSuccessListener {
        Log.d(TAG, "Geofence added")
    }
    addOnFailureListener {
        Log.e(TAG, "Failed to create geofence")
    }
} 

Broadcast Receiver:

class GeofenceBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        Log.d(TAG, "onReceive")
    }
}

Receiver in Manifest:

<receiver android:name=".GeofenceBroadcastReceiver">
    <intent-filter>
        <action android:name="com.example.GEOFENCE_TRANSITION"/>
    </intent-filter>
</receiver>

Thanks

EDIT: IntentService version

Pending Intent:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(context, GeofenceIntentService::class.java)
    PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Intent Service:

class GeofenceIntentService : IntentService("GeofenceIntentService") {
    override fun onHandleIntent(p0: Intent?) {
        Log.d(TAG, "onHandleIntent")
    }
}

Service in Manifest:

<service android:name=".GeofenceIntentService"/>
like image 286
Federico Matera Avatar asked Jun 21 '18 09:06

Federico Matera


People also ask

How does Android Oreo affect apps with geolocation functionality?

When an app is in the background, it is given several minutes to create and use services, but at the end of that time slot, the app is considered idle and the OS will stop running background services. These changes directly affect apps with geolocation functionality. Android Oreo limits how frequently apps can gather location in the background.

How to create and monitor geofences?

Create and monitor geofences 1 Set up for geofence monitoring. The first step in requesting geofence monitoring is to request the necessary permissions. ... 2 Create and add geofences. ... 3 Handle geofence transitions. ... 4 Stop geofence monitoring. ... 5 Use best practices for geofencing. ... 6 Troubleshoot the geofence entrance event. ...

What are the limitations of Android Oreo?

Android Oreo limits how frequently apps can gather location in the background. Background apps can only receive location updates a few times each hour. The APIs affected due to these limits include Fused Location Provider, Geofencing, Location Manager, Wifi Manager, GNSS Measurements and GNSS Navigation Messages.

How often does my Android device respond to geofence events?

Note: On Android 8.0 (API level 26) and higher, if an app is running in the background while monitoring a geofence, then the device responds to geofencing events every couple of minutes. To learn how to adapt your app to these response limits, see Background Location Limits .


1 Answers

You should get an Intent every couple of minutes on Android 8 when your geofence transition is reached in background.

See: https://developer.android.com/training/location/geofencing#java

Handle geofence transitions When Location Services detects that the user has entered or exited a geofence, it sends out the Intent contained in the PendingIntent you included in the request to add geofences. This Intent is received by a service like GeofenceTransitionsIntentService, which obtains the geofencing event from the intent, determines the type of Geofence transition(s), and determines which of the defined geofences was triggered. It then sends a notification as the output.

Note: On Android 8.0 (API level 26) and higher, if an app is running in the background while monitoring a geofence, then the device responds to geofencing events every couple of minutes. To learn how to adapt your app to these response limits, see Background Location Limits.

Once the geofence service is registered it´s still there and you have nothing else to do and only check your IntentService for the specific PendingIntent, exclude when the device is rebooted you need to reregister your geofence service.

Also check: https://developer.android.com/about/versions/oreo/background-location-limits

like image 51
edhair Avatar answered Oct 07 '22 22:10

edhair