0

When I request a permission (in this case Manifest.permission.ACCESS_FINE_LOCATION or "android.permission.ACCESS_FINE_LOCATION"), there is no dialog shown and the permission is always denied. When I check with ActivityCompat.shouldShowRequestPermissionRationale, it returns false, too. Indicating that the user either never was asked (what seams to be the problem here) or checked "never show again". The App Info displays "no permissions required".

My problem is related to this, but I already asked for permission as sugested in the answer.

Here is some of my code:

// firstly I call this function to check and request the permissions
private void checkPermissions(final String[] permissions) {
    for(final String permission : permissions) {
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, permission)) {
            ActivityCompat.requestPermissions(this, new String[]{permission}, PERMISSION_REQUEST);
        }
    }
}

// This results in this callback to be called. No dialog is shown.
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST: {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    // The permmission is always denied ...
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
                        // and we should not show a request. We always end up here
                    } else {
                        // here we should show the request again, but we dont end up here
                    }
                }
            }
        }
    }
}

I'm thankfull for any hint what I'm overlooking here.

Myon
  • 937
  • 13
  • 23

1 Answers1

0

what contains your String[] permissions array? you should call requestPermissions only once, passing array with all needed (not granted) perms, instead of calling this method with every needed perm separately. maybe another permission is denied, which is before location one, and request for location permission isn't going out, as Activity loose focus and further/multiple requestPermissions are no-op

private void checkPermissions(final String[] permissions) {
    ArrayList<String> neededPerms = new ArrayList<>();
    for(final String permission : permissions) {
        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, permission)) {
            neededPerms.add(permission);
        }
    }
    if(neededPerms.size()>0){
        String[] array = new String[neededPerms.size()];
        neededPerms.toArray(array);
        ActivityCompat.requestPermissions(this, array, PERMISSION_REQUEST);
    }
}
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thanks for the hint. I request {ACCESS_FINE_LOCATION, BLUETOOTH, BLUETOOTH_ADMIN} for my BLE App. However only ACCESS_FINE_LOCATION is not given (for some reason) and ´ActivityCompat.requestPermissions´ is only called once. But I'll add that to prevent future errors. But the behavior does not change and the problem persists... – Myon Jan 07 '21 at 12:18