Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onConnected() not being called to get location updates (GooglePlayApi for location)

So i have this code:

package com.entu.bocterapp;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class LocationManager implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Context mContext;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LocationRequest mLocationRequest;

public LocationManager(Context context) {
    mContext = context;
    //
    if (checkIfGooglePlayServicesAreAvailable()) {
        //Get Access to the google service api
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    } else {
        //Use Android Location Services
        //TODO:
    }
}

public Location getCoarseLocation() {
    if (mLastLocation != null) {
        return mLastLocation;
    } else return null;
}

private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(mContext)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

private boolean checkIfGooglePlayServicesAreAvailable() {
    int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (errorCode != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(errorCode, (RecentSightings) mContext, 0).show();
        return false;
    }
    return true;
}


@Override
public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location != null) {
        mLastLocation = location;
        Toast.makeText(mContext, location.getLongitude() + " , " + location.getLatitude() + " : " + location.getAccuracy(), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(mContext, "suspended", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

I am trying to get location in this class, but onConnected() never gets called(i waited for 1-2 minutes). I went with the debugger, it says google play services are available.

Does anyone know what i'm doing wrong? I'm stuck here for hours, reading everything, and can't get it to work.

Cheers!

like image 563
Vlad Iancu Avatar asked Feb 03 '15 14:02

Vlad Iancu


3 Answers

You must call

mGoogleApiClient.connect();
like image 64
Abedalkareem Omreyh Avatar answered Nov 14 '22 21:11

Abedalkareem Omreyh


try this:

@Override
protected void onStart() {
    if(mGoogleApiClient!=null){

        mGoogleApiClient.connect();
    }
    super.onStart();
}

@Override
protected void onStop() {
    if(mGoogleApiClient!=null){

        if(mGoogleApiClient.isConnected()){

            mGoogleApiClient.disconnect();
        }

    }

    super.onStop();
}
like image 28
Bhriguraj Salponia Avatar answered Nov 14 '22 23:11

Bhriguraj Salponia


I got the same problem just updated my grade version :10.0.1' to

compile 'com.google.android.gms:play-services-location:10.2.1'

it works.

like image 25
Sohail Zahid Avatar answered Nov 14 '22 22:11

Sohail Zahid