Permission location Android Xamarin

Luigui Flores 1 Reputation point
2021-03-09T13:46:22.247+00:00

Hello I have the following code to ask location on runtime for a Xamarin forms (Android)

var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
        if (status != PermissionStatus.Granted)
        {
            if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
            {
             await   Application.Current.MainPage.DisplayAlert("Need location", "Gunna need that location", "OK");
            }

            status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
        }

But after the request alert pops up, the app gets stuck and I need restart it. after that works normally and don't ask for the permission again. I have added to the manifest btw thx in advance

Developer technologies | .NET | Xamarin
Developer technologies | .NET | .NET Runtime
Developer technologies | .NET | .NET CLI
{count} votes

2 answers

Sort by: Most helpful
  1. Joe Manke 1,091 Reputation points
    2021-03-09T19:26:44.82+00:00
    1 person found this answer helpful.

  2. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-03-10T02:49:58.197+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Try to change the code in OnRequestPermissionsResult method .

    From

    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    to

    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    And you have to install Plugin.Permissions package first .

    Refer to the thread : https://forums.xamarin.com/discussion/comment/352856/#Comment_352856.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.