0

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)?

1 Answers1

1

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.

Homayoon Ahmadi
  • 1,181
  • 1
  • 12
  • 24
  • So, we wouldn't ask for turning on the location service? (APIs 21 and 22). Are granting permission and asking to turn on a service different from each other? – user21193451 Jul 24 '23 at 09:12
  • No, you don't need to ask for permissions in runtime for API level 22 and lower. Asking for location permission and asking for turning on GPS are different. – Homayoon Ahmadi Jul 24 '23 at 09:21
  • So, How do I know that whether location is on or off? – user21193451 Jul 24 '23 at 10:36
  • Thanks for directing me to the correct question. [this post](https://stackoverflow.com/questions/10311834/how-to-check-if-location-services-are-enabled) solved my question. – user21193451 Jul 24 '23 at 11:02
  • Yes you can use `isProviderEnabled` function of `LocationManager`. Happy to help! – Homayoon Ahmadi Jul 24 '23 at 11:21