reza hemati
reza hemati

Reputation: 21

error permision ACCESS_FIND_LOCATION

I'm using permission ACCESS_FINE_LOCATION but when run.error:requires ACCESS_FINE_LOCATION permission why?

thanks in advance.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Upvotes: 1

Views: 130

Answers (2)

AskNilesh
AskNilesh

Reputation: 69734

ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION both permission required when to trying to get location so add this both permission in manifest file as well as if you targeting android 6.0 and above than add this runtime permsiion like below code

   String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
            if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.
                    checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
                        {permission}, PERMISSION_GPS_CODE);

            }

now handle permisiion result

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_GPS_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {


            Toast.makeText(this, "location_permission_granted ", Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(this, "location_permission_not_granted ", Toast.LENGTH_SHORT).show();
        }
    }

}

Upvotes: 2

Danger
Danger

Reputation: 453

Did you write run time permissions in your code? As from Android 6.0, for every permission stated in manifest, must also be asked run time from user.

private void requestLocationPermission() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
    }
}

Also handle user's response as well:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case LOCATION_PERMISSION_CODE: {
            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}

Upvotes: 1

Related Questions