1
votes

I'm trying to request location updates from google play services on my android app. So far I'm able to get last location if i start google maps first, but not able to get any updates.

public class Inbox extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks, 
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int r = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
        if(r != 0){
            System.out.println(r);
            Dialog d = GoogleApiAvailability.getInstance().getErrorDialog(this,r,1);
            d.show();
        }
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .addApi(AppIndex.API).build();
        }
        mGoogleApiClient.connect();
        super.onStart();
    }

    @Override
    public void onConnected(Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            System.out.println("Problem with rights");
        }
        else{
            Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            LocationRequest mLocationRequest = new LocationRequest();

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

            if(mLastLocation != null){
                System.out.println(mLastLocation.toString());
            }
            else {
                System.out.println("No location cached");
            }
            LocationAvailability a = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
            System.out.println(a);
        }
    }


    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }
    @Override
    public void onLocationChanged(Location location) {
        System.out.println(location.toString());
    }




    @Override
    public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
        Action viewAction = Action.newAction(...);
        AppIndex.AppIndexApi.start(mGoogleApiClient, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();
        Action viewAction = Action.newAction(...);
        AppIndex.AppIndexApi.end(mGoogleApiClient, viewAction);
        mGoogleApiClient.disconnect();
    }

}

When i start the app after using google maps, i get the last cached location from:

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

But right after, i print out that location services are unavailable with this:

LocationAvailability a = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
System.out.println(a);

onLocationChanged/onConnectionFailed/onConnectionSuspended are never called.

I'm running my app on a virtual Nexus 6 with android 5.1.0 on Genymotion.

1

1 Answers

2
votes

I think you missed a few things here. In you onCennected callback, you have to first check if you can get last known location. If it is not available then you have to request location updates. Refactor your onConnected as shown below and it should work fine

@Override
    public void onConnected(Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, 
           Manifest.permission.ACCESS_FINE_LOCATION) != 
           PackageManager.PERMISSION_GRANTED && 
           ActivityCompat.checkSelfPermission(this, 
           Manifest.permission.ACCESS_COARSE_LOCATION) != 
           PackageManager.PERMISSION_GRANTED) {
               System.out.println("Problem with rights");
        } else {

           Location lastLocation = LocationServices.FusedLocationApi
                                       .getLastLocation(mGoogleApiClient);

           if (lastLocation != null) {
            //you have a location. use that here. no need to request for location 
            // updates
            mGoogleApiClient.disconnect();
           } else {
            //you have to request for location
             mLocationRequest = LocationRequest.create();
             mLocationRequest.setInterval(2000);
             mLocationRequest.setFastestInterval(1000);
             mLocationRequest.setSmallestDisplacement(50);
             mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
             LocationServices.FusedLocationApi
               .requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

           }

        }
    }

And in your locationChanged callback do the following

@Override
public void onLocationChanged(Location location) {

    //now that we got the initial location. it is time to stop the api client
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    mGoogleApiClient.disconnect();

   // use your newly obtained location here

}

Note : If you try this on GenyMotion. Make sure it can receive location by enabling GPS on the GM emulator