0

I have a background service which runs in the background tracking device location using a PendingIntent.

The service calls mFusedLocationClient.requestLocationUpdates(mLocationRequest, locationRequest, pendingIntent) to start location tracking but also sometimes to update the location tracking (change the frequency of updates).

Should I store the GoogleApiClient instance in the service class or should I connect to it each time I need it?

  • I'm worried if I store it, disconnection can appear. This shouldn't be a problem in theory because the documentation says GoogleApiClient automatically tries to reconnect itself.

  • If I connect each time to GoogleApiClient, it adds a bit of complexity in the code, because the connection made with connect() is asynchronous.

Based on your experience, what do you think is best?

Don Box
  • 3,166
  • 3
  • 26
  • 55

1 Answers1

0

You should call googleApiClient.connect() in onStart() of your activity and googleApiClient.disconnect() in onStop(). You can implement GoogleApiClient.ConnectionCallbacks, which will let you know if the client connects successfully via invoking public void onConnected(Bundle bundle) and then you can access your api.

Amit Kumar
  • 1,428
  • 2
  • 12
  • 20
  • Thanks. I updated the description. I have a background service. I am worried about disconnection. – Don Box Jul 08 '17 at 08:53