Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geofence transition PendingIntent blocked by the OS on Android Oreo

This only happens on Android Oreo. I'm using Play Services 11.4.2. I'm registering geofences using the GeofencingClient and the addGeofences method with a pendingIntent to an IntentService that is handling geofence transitions. It looks like the intent sent by the play services is blocked by the OS in certain conditions.

The system logs the following:

Background start not allowed: service Intent { cmp=my.app.id/my.package.struct.GeofenceTransIntentService (has extras) } to my.app.id/my.package.struct.GeofenceTransIntentService from pid=-1 uid=10154 pkg=my.app.id

as soon as I add a geofence in the following situations:

  • when I add it after the device boots
  • when I add it after the app was swiped In both occasions the app is actually already running in the background (since I'm able to run the code that adds the geofence) because I listen to the PROVIDERS_CHANGED, BOOT_COMPLETED.
like image 913
stefano cattaneo Avatar asked Dec 04 '25 18:12

stefano cattaneo


2 Answers

This is caused by the new Android Oreo background service limitations.

You have to change the PendingIntent from using a Service to using a BroadcastReceiver.

See this CodeLab for further information and example code.

like image 123
pincopallino Avatar answered Dec 06 '25 07:12

pincopallino


In addition to switching to a BroadcastReceiver as mentioned in the accepted answer, I'd like to point out that although you will still receive BOOT_COMPLETED, you will not be able to receive PROVIDERS_CHANGED any longer.

PROVIDERS_CHANGED is an implicit broadcast and is not in the list of broadcast exceptions.

BOOT_COMPLETED is in the list of broadcast exceptions which is why you'll still receive it.

If you want to learn about an alternative approach to get the hook when location services is toggled, please see my answer to a related question for more details.

like image 21
Michael Krause Avatar answered Dec 06 '25 07:12

Michael Krause



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!