JDenais JDenais - 4 months ago 27x
Android Question

google api client callback is never called

I am trying to get the last known location using google services API, but after I build the GoogleApiClient, no callback method is ever fired.

My activity looks like that :

public class MainActivity extends Activity implements FragmentObserver, SessionSpotListObserver,
ConnectionCallbacks, OnConnectionFailedListener{

//Objects used for the location API
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Unique tag for the error dialog fragment
private static final String DIALOG_ERROR = "dialog_error";
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;
public static final String STATE_RESOLVING_ERROR = "resolving_state";
//Request code to use when launching the activity to fix the connection to google API
private static final int REQUEST_SOLVE_CONNEXION = 999;

protected void onCreate(Bundle savedInstanceState) {

//We make sure that google play service is available on the device
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS){
//We get a connection to the Google Play Service API to get the location of the user
else {

public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
mResolvingError = true;

// The rest of this code is all about building the error dialog

/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
// Create a fragment for the error dialog
ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt(DIALOG_ERROR, errorCode);
dialogFragment.show(getFragmentManager(), "errordialog");

/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
mResolvingError = false;

/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() { }

public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt(DIALOG_ERROR);
return GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), REQUEST_RESOLVE_ERROR);

public void onDismiss(DialogInterface dialog) {

public void onConnected(Bundle arg0) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
Log.d("API Connection", "The API has connected and the last location is :" + mLastLocation);
if (mLastLocation != null) {



public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub


* Creates the connexion to the Google API. Once the API is connected, the
* onConnected method is called.
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)

I placed breakpoints on all callback methods, that is how I know that none is called.

Because at this stage I am not using Google Map Api, I did not register my app to get a key. Do I need to do that even if I just get the location ?

Don't hesitate to tell me if you need more info.

Thank you all.


You never call mGoogleApiClient.connect() after building your GoogleApiClient. Your onCreate() should instead be:

protected void onCreate(Bundle savedInstanceState) {

Note that there is no need to call GooglePlayServicesUtil.isGooglePlayServicesAvailable() if you are using GoogleApiClient as connect() includes that check as well.