Popup was not shown when execute Runtime Permission in the Xamarin Android Application

MohanasundharamMohanasundharam Member ✭✭✭
edited April 24 in Xamarin.Android

I have used the Runtime permission method in my code, but the Pop-up is not showing in the Application, one more thing is when i check in live device settings where the permissions are automatically enabled. How is it possible? and how can i show the popup?

//My Code is Here,

public static int REQUEST_LOCATION = 99;

        if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessFineLocation))
        {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.
            Log.Info(TAG, "Displaying camera permission rationale to provide additional context.");

            var requiredPermissions = new String[] { Manifest.Permission.AccessFineLocation };
            ActivityCompat.RequestPermissions(this, requiredPermissions, REQUEST_LOCATION);
        }
        else
        {
            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessCoarseLocation }, REQUEST_LOCATION);
        }


    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        if (requestCode == REQUEST_LOCATION)
        {
            Log.Info(TAG, "Received response for Location permission request.");
            if ((grantResults.Length == 1) && (grantResults[0] == Permission.Granted))
            {
                Log.Info(TAG, "Location permission has now been granted.");
            }
            else
            {
                Log.Info(TAG, "Location permission was NOT granted.");
            }
        }
        else
        {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

// Permissions in Manifest.Xml File

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

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 25

    For this application, have you granted this permissions before?
    I think the code you requested permission for is not quite correct. When you request the two permissions, you can try the following code:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
           static string[] PERMISSIONS_LOCATION = {
            Manifest.Permission.AccessCoarseLocation,
            Manifest.Permission.AccessFineLocation
        };
    
    private void showLocation()
        {
            Log.Info(TAG, "Show location button pressed. Checking permissions.");
    
            // Verify that all required contact permissions have been granted.
            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted
                || ActivityCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != (int)Permission.Granted)
            {
                // locations permissions have not been granted.
                Log.Info(TAG, "location permissions has NOT been granted. Requesting permissions.");
                RequestLocationPermissions();
            }
            else
            {
                // location permissions have been granted. 
                Log.Info(TAG, "location permissions have already been granted. Displaying location details.");
                //
            }
        }
    

    method: RequestLocationPermissions

    void RequestLocationPermissions()
        {
            if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessCoarseLocation)
                || ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessFineLocation))
            {
                Log.Info(TAG, "Displaying location permission rationale to provide additional context.");
    
                // Display a SnackBar with an explanation and a button to trigger the request.
                Snackbar.Make(layout, Resource.String.permission_location_rationale,
                    Snackbar.LengthIndefinite).SetAction(Resource.String.ok, new Action<View>(delegate (View obj) {
                        ActivityCompat.RequestPermissions(this, PERMISSIONS_LOCATION, REQUEST_LOCATION);
                    })).Show();
            }
            else
            {
                ActivityCompat.RequestPermissions(this, PERMISSIONS_LOCATION, REQUEST_LOCATION);
            }
        }
    

    method: OnRequestPermissionsResult

      public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
          if (requestCode == REQUEST_LOCATION)
            {
                Log.Info(TAG, "Received response for location permissions request.");
    
               // We have requested multiple permissions for location, so all of them need to be checked
                if (PermissionUtil.VerifyPermissions(grantResults))
                {
                   Snackbar.Make(layout, Resource.String.permission_available_location, Snackbar.LengthShort).Show();
                }
                else
                {
                    Log.Info(TAG, "locations permissions were NOT granted.");
                    Snackbar.Make(layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show();
                }
    
            }
            else
            {
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    

    class PermissionUtil

     public  class PermissionUtil
    {
        /**
    * Check that all given permissions have been granted by verifying that each entry in the
    * given array is of the value Permission.Granted.
    *
    * See Activity#onRequestPermissionsResult (int, String[], int[])
    */
        public static bool VerifyPermissions(Permission[] grantResults)
        {
            // At least one result must be checked.
            if (grantResults.Length < 1)
                return false;
    
            // Verify that each required permission has been granted, otherwise return false.
            foreach (Permission result in grantResults)
            {
                if (result != Permission.Granted)
                {
                    return false;
                }
            }
            return true;
        }
    }
    

    Note: when you have granted the permissions for an app , you can uninstall the app and install it again, then you can test this function(runtime permission) again.

  • jezhjezh Member, Xamarin Team Xamurai

    @Mohanasundharam
    Have you resolved this question?

  • MohanasundharamMohanasundharam Member ✭✭✭
    edited April 26

    No @jezh , still this issue is coming. Actually, I refer this link, https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows - for runtime permissions for location, but why i'm not getting the popup and why it enabled the permissions automatically from the application when i check settings->apps -> my App -> Permissions

  • jezhjezh Member, Xamarin Team Xamurai
    edited April 29

    What Android device are you using ?
    And what's android version of your android device?
    When we check the official document , we will find:

    Prior to Android 6.0, you could reasonably assume that if your app is running at all, it has all the permissions it declares in the app manifest. 
    

    So if your android version of your device is prior to Android 6.0, it is normal to enabled the permissions automatically from the application.

    For more details:
    https://www.howtogeek.com/230683/how-to-manage-app-permissions-on-android-6.0/

  • MohanasundharamMohanasundharam Member ✭✭✭

    I'm Using Google Pixel 9.0

  • jezhjezh Member, Xamarin Team Xamurai

    Have you installed the app before? If so, try uninstalling it and reinstalling it.

  • jezhjezh Member, Xamarin Team Xamurai

    Have you resolved this question?

Sign In or Register to comment.