[Xamarin.Android] Permissions.RequestAsync never returns

Adrien COURNAND 141 Reputation points
2021-06-26T18:31:34.927+00:00

When I ask for recording permission at runtime, Permissions.RequestAsync never returns.

I added these lines to my MainActivity.cs files:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
        {  
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
        }  

And I have this setup on another file to ask permission:

        private async Task<bool> AskRecordingPermissionAsync()  
        {  
            var status = await Permissions.CheckStatusAsync<Permissions.Microphone>();  
  
            if (status != PermissionStatus.Granted)  
                status = await Permissions.RequestAsync<Permissions.Microphone>();  
  
            return status == PermissionStatus.Granted;  
        }  
  
        public bool AskRecordingPermission()  
        {  
            bool hasRecordingPermission = true;  
  
            if (CURRENT_ANDROID_VERSION >= 23)  
            {  
                hasRecordingPermission = AskRecordingPermissionAsync().Result;  
            }  
  
            return hasRecordingPermission;  
        }  

I also asked for the static RECORD_AUDIO permission :

109596-image.png

But when I step through the execution, it always blocks at Permissions.RequestAsync. I have a popup that appears to ask me to allow or disallow the permission. When I allow it, the application is stuck on a black screen. I have to restart the application to have it working properly once the permission accepted.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,191 Reputation points Microsoft External Staff
    2021-06-28T07:19:33.107+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Please do not use .Result way to get the value. It will hang on your UI, you can get the value like following code with await .

       public partial class MainPage : ContentPage  
           {  
               public MainPage()  
               {  
                   InitializeComponent();  
               }  
         
               private async void Button_Clicked(object sender, EventArgs e)  
               {  
                   //Get the value from here.  
         
                   var status=   await  AskRecordingPermission();  
               }  
         
               private async Task<bool> AskRecordingPermissionAsync()  
               {  
                   var status = await Permissions.CheckStatusAsync<Permissions.Microphone>();  
         
                   if (status != PermissionStatus.Granted)  
                       status = await Permissions.RequestAsync<Permissions.Microphone>();  
         
                   return status == PermissionStatus.Granted;  
               }  
         
               public async Task<bool> AskRecordingPermission()  
               {  
                  var hasRecordingPermission = await AskRecordingPermissionAsync();  
                  return hasRecordingPermission;  
               }  
         
           }  
    

    Best Regards,

    Leon Lu


    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.