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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,295 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,125 questions
{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,751 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.