9

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) {
        super.onCreate(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
            buildGoogleApiClient();
         }
         else {
             GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
                     this,
                     REQUEST_SOLVE_CONNEXION); 
         }
}

@Override
    public void onConnectionFailed(ConnectionResult result) {
         if (mResolvingError) {
                // Already attempting to resolve an error.
                return;
            } 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.
                    mGoogleApiClient.connect();
                }
            } else {
                // Show dialog using GooglePlayServicesUtil.getErrorDialog()
                showErrorDialog(result.getErrorCode());
                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.setArguments(args);
            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() { }

            @Override
            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);
            }

            @Override
            public void onDismiss(DialogInterface dialog) {
                ((MainActivity)getActivity()).onDialogDismissed();
            }
    }

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

            }


    }

    @Override
    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)
        .addApi(LocationServices.API)    
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    }

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.

JDenais
  • 2,956
  • 2
  • 21
  • 30

2 Answers2

42

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

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    buildGoogleApiClient();
    mGoogleApiClient.connect();
}

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

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I didnt know the connect method performed the check automatically. Good catch. – Eugen Pechanec Mar 16 '15 at 02:22
  • 3
    Thank you mate. It hurts my feelings a bit to have missed the connect() method in the Api client =) Maybe google should make it more obvious int the training documentation because I really was under the impression that the Builder was making the connection. – JDenais Mar 16 '15 at 02:31
  • THanks i recently found the issue and this answer helped me out :) – Hardy Apr 20 '15 at 05:31
  • 1
    omfg, I read google docs for over an hour and they never use this method. Thank you! – Jason Cheladyn Oct 10 '15 at 05:25
  • For me it was perfectly fine before some hours in development, Connection & then score post. But then it never ever calling success/failure callback. What could be reason. Is there any limit on number of of connection using an email-id ? I'm using _com.google.android.gms:play-services:9.8.0'_ & _com.google.gms:google-services:3.0.0_. – CoDe Nov 20 '16 at 00:30
  • 1
    So in my case now _GoogleApiClient.connect()_ not working at all, what could be reason, please suggest !! – CoDe Nov 20 '16 at 00:36
0

consider calling onLocationChanged() and passing it's Location parameter to mLastLocation for continuous location update when the user location changes. Also you might want to reduce the drain on your battery by setting LocationRequest() interval and distance to a small value.

cooljay
  • 31
  • 3