Search code examples
javaandroidandroid-fragmentsandroid-permissionsdeprecated

Android API 21 Location Permission condition is true before accept dialog result


I changed the following method

public static boolean requestForLocationPermission(Fragment fragment) {
String LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION;
     if (ContextCompat.checkSelfPermission(fragment.requireContext(), LOCATION_PERMISSION)
                    == PackageManager.PERMISSION_GRANTED)
                return true;
    
            if (ActivityCompat.shouldShowRequestPermissionRationale(fragment.requireActivity(), LOCATION_PERMISSION)) {
                PermissionDialog.showForResult(fragment, LOCATION_PERMISSION_REQUEST_CODE, LOCATION_PERMISSION);
            } else {
                fragment.requestPermissions(new String[]{LOCATION_PERMISSION}, LOCATION_PERMISSION_REQUEST_CODE);
            }
            return false;
}

to the following code after I found out that auto location detection doesn't work on API 21 and mainly because fragment.requestPermissions is deprecated:

public static boolean requestForLocationPermission(Fragment fragment) {

String LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(fragment.requireContext(), LOCATION_PERMISSION)
                == PackageManager.PERMISSION_GRANTED) {
            Log.d("request_Permission 23:", "has been granted!");
            return true;
        }
    } else {
        if (PermissionChecker.checkSelfPermission(fragment.requireContext(), LOCATION_PERMISSION)
                == PermissionChecker.PERMISSION_GRANTED) {
            Log.d("request_Permission 21:", "has been granted!");
            return true;
        }
    }
    if (ActivityCompat.shouldShowRequestPermissionRationale(fragment.requireActivity(), LOCATION_PERMISSION)) {
        PermissionDialog.showForResult(fragment, LOCATION_PERMISSION_REQUEST_CODE, LOCATION_PERMISSION);
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            fragment.requireActivity().requestPermissions(new String[]{LOCATION_PERMISSION}, LOCATION_PERMISSION_REQUEST_CODE);
        } else {
            ActivityCompat.requestPermissions(fragment.requireActivity(),
                    new String[]{LOCATION_PERMISSION},
                    LOCATION_PERMISSION_REQUEST_CODE);
            Log.d("request_Permission:", "has been requested ... ");
        }
    }
    return false;
}

After adding these Log lines, it was very odd that for API 21 in the logcat it is showing request_Permission 21: has been granted! when the permission dialog appear on screen without any option-selection.

So why is PermissionChecker.checkSelfPermission(fragment.requireContext(), LOCATION_PERMISSION) == PermissionChecker.PERMISSION_GRANTED always true for API 21 (and probably for API 22)?


Solution

  • Runtime permissions were introduced in Android 6.0 (API level 23) with the release of Android Marshmallow. Before Android 6.0, permissions were granted at the time of installation, and users had no control over individual permissions.

    Starting from Android 6.0, users have the ability to grant or deny individual permissions at runtime, giving them more control over their privacy and allowing developers to request permissions when they are actually needed by the app.