user648462
user648462

Reputation: 655

Accessing Google Play Services Location API in a Service

I want to connect to the Google Play Services Location API in a service so I can request location updates and geofencing. The example on the Android developer site makes the connection in an Activity using the activity lifecycle methods to connect and disconnect, it also handles failure by displaying dialogs etc. In my app I want to use a service rather than an Activity as I want to keep receiving location updates when the activity isn't visible. I am planning on connecting to Google Play Services when the service is started and disconnecting when I no longer require location updates or when the service is stopped. Is it possible to handle the connection failure scenarios from the service as it doesn't have access to an Activity to display the dialogs etc?

Thanks

Upvotes: 1

Views: 399

Answers (1)

You can use a LocalBroadcastReceiver in the activity, and let the service send the messages when a failure scenario is reached. The activity can show error accordingly, based on the provided intent and other parameters that you can send within it. Note that you have to validate, inside the receiver, when the activity is on background, and show the error or try when the user brings back your activity to foreground.

Upvotes: 1

Related Questions