0

Is there any method to identify which location permission is granted by user in programmatically in Android.

like the following

  • While using the app
  • Only this time
  • Deny

I want to know a particular applications current location permission status from another app. Not for checking its run time.

jerald jacob
  • 613
  • 1
  • 4
  • 18

2 Answers2

2

Yes, we can check it programmatically using the combination of permissions. I have created the method on the basis of the android version :

 private void checkLocationPermissionState(Context context){

    int fineLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
    int coarseLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

        int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        boolean preciseLocationAllowed = (fineLocation == PackageManager.PERMISSION_GRANTED)
                && (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (preciseLocationAllowed) {
            Log.e("PERMISSION","Precise location is enabled in Android 12");
        } else {
            Log.e("PERMISSION","Precise location is disabled in Android 12");
        }

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location is allowed all the time");
        } else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
            Log.e("PERMISSION","Location is allowed while using the app");
        }else{
            Log.e("PERMISSION","Location is not allowed.");
        }

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

        int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);

        boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location is allowed all the time");
        } else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
            Log.e("PERMISSION","Location is allowed while using the app");
        }else{
            Log.e("PERMISSION","Location is not allowed.");
        }
        
    } else {

        boolean isAppLocationPermissionGranted = (fineLocation == PackageManager.PERMISSION_GRANTED) &&
                (coarseLocation == PackageManager.PERMISSION_GRANTED);

        if (isAppLocationPermissionGranted) {
            Log.e("PERMISSION","Location permission is granted");
        } else {
            Log.e("PERMISSION","Location permission is not granted");
        }
    }


}
Himanshi Thakur
  • 2,077
  • 14
  • 32
  • Hi @Himanshi thanks for the answer. I know this method will give us to know the permissions which enabled the user while run time. But I want to know when the app is in the rest mode and how can we know the particular application currently using which location permission. (Like I want to know a particular applications current location permission status from another app) – jerald jacob Mar 07 '22 at 11:45
  • Both apps belong to you or do you want to check the permission status in another developer app? – Himanshi Thakur Mar 07 '22 at 13:07
  • I want to know another developer app or apps – jerald jacob Mar 08 '22 at 05:38
  • check this link , it might help you https://stackoverflow.com/a/37181733/9942608 – Himanshi Thakur Mar 08 '22 at 06:01
0

yes in the setting

enter image description here

hossein
  • 638
  • 1
  • 3
  • 14
  • Hi @hossein thanks for the update. Actually i want to know it programmatically, I just update the question can you have a look it again. – jerald jacob Mar 02 '22 at 10:52