Search code examples
androidlocationlocation-client

Android LocationClient onDisconnect doesn't get called


From the GooglePlayServicesClient.ConnectionCallbacks documentation onDisconnected should get called when the LocationClient is disconnected. From my testing I noticed it never gets called if I disconnect the LocationClient myself.

Here is the most basic sample code I have.

public class MainActivity extends ActionBarActivity implements GooglePlayServicesClient.ConnectionCallbacks,
                                                               GooglePlayServicesClient.OnConnectionFailedListener {

  private LocationClient locationClient;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationClient = new LocationClient(getApplicationContext(), this, this);
    locationClient.connect();
  }

  @Override
  public void onConnected(Bundle bundle) {
    Log.d("APP", "Location Client Connected");
    locationClient.disconnect();
    Log.d("APP", "Client is connected " + locationClient.isConnected());
  }

  @Override
  public void onDisconnected() {
    Log.d("APP", "Location Client Disconnected");
  }

  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d("APP", "Location Client connect failed");
  }
}

I received the onConnected() callback but never the onDisconnected() callback. In the onConnected callback where I print out the connected status of the client, it properly prints out "false".

Am I misunderstanding something or is the onDisconnected callback only called by external events such as:

  • activity/service shutdown
  • location client becomes unavailable

Solution

  • I was stuck with the same issue and found an answer here:

    I guess it's a bit late but I was wondering the same, so I'll reply.

    It's normal behaviour. It's not clear from the API documentation, but in the official tutorial at http://developer.android.com/training/location/retrieve-current.html, it says about onDisconnected:

    Called by Location Services if the connection to the location client drops because of an error.

    Which means it shouldn't be called if you call disconnect() yourself.