Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

LocationServices permission problem in "I Am Here" app.

I have a problem in "i Am Here" app which i made following this guide.

In this onConnected() method:

@Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.i(TAG, "Location services connected.");
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        } else {
             handleNewLocation(location);
        }
    }

I have an error which says 'Call requires permission which can be rejected by user...' I found a way to modify code:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
       location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

But app is not working. I guess I have to add an else statement to handle somehow the case when permission is not granted.

Ben Deitch
Ben Deitch
Treehouse Teacher

Would you happen to be on API 23 (Marshmallow)? If you are, then you'll also need to explicitly request that permission. https://developer.android.com/training/permissions/requesting.html

According to this link I should only ask for permission in onConnected() method and I should handle all location operations in onRequestPermissionsResult() method ? And it would still work on devices with lower API ?