Elior
Elior

Reputation: 3266

Geofence Transitions is not triggered when application is on background

I'm trying to work with geofences in my application. So far I have succeeded and it works perfectly when the application is in foreground. I'm checking with Android Emulator which have google play services. To check this I'm setting the location parameters and as I said it works fine when the application is on foreground.

But when the application is on background it seems that the transitions are not triggered, until I open the application or open Google Maps application.

So I've tried to use BroadcastReceiver instead of IntentService and I've registered the broadcastreceiver in the manifest file Like this:

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

But I'm still having the same results..

So how can I trigger the Geofence transitions when the application is on background?

I thought about using LocationServices.FusedLocationApi.requestLocationUpdates inside a Service. So with this approach my application will ask for location updates and this probably will trigger the geofence transition.

But probably it will consume battery and this is a bad practice. So, are there any other options?

Upvotes: 2

Views: 983

Answers (1)

Elior
Elior

Reputation: 3266

OK. So it seems to be the new Android O background limitations due to battery issues. In Android 8, they have "New Rules" about Background processes (services and etc), in order to reduce battery consumption. In Android 8, when the application is not on foreground (i.e user pressed on Home button) your services will probably will be killed even they are background services, in case you don't have any component in foreground that related to your app.

Everything will work fine if your device system is Android 7 and you have targeted your app to Android 7 and lower.

Regarding location updates, in order to save battery consumption, the updates will occur few times in each hour.

They suggest to use foreground service so your application will still considered as foreground.

see this link

Upvotes: 2

Related Questions