Cannot find a way to request permission at runtime on android

Adrien COURNAND 141 Reputation points
2021-06-07T18:55:39.627+00:00

I am making an application for Android. I need to ask permission to record Audio at runtime because it is a dangerous permission. I followed the Android way, but I cannot find the function androidx.core.app.ActivityCompat.requestPermission : I only have the namespace AndroidX.Core.Graphics.

I also tried to follow this tutorial, I installed the mentioned nugget package, but I could not find the functions ContextCompat.CheckSelfPermission. I tried many possibilities but I never saw IntelliSense proposing it. What namespace should I include to get the functions of the nugget package ? Why I cannot find the functions I need inside the AndroidX namespace?

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-08T05:24:20.62+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to use AndroidX package, you should migrate your application to AndroidX, please refer to the following doc step by step.

    https://learn.microsoft.com/en-us/xamarin/android/platform/androidx

    After that, please set the Target Framework to Android 10 or later like following screenshot.

    xxxx

    Then you can get the AndroidX packages.

    103282-image.png

    Another way to request permission. If you use Xamarin. I recommand you to use Xamarin.Essentials: Permissions. install Xamarin.Essentials nuget packages(If you did not install it).

    Then Use following code in your activity.

       public class MainActivity : AppCompatActivity  
           {  
               public int REQUEST_LOCATION;  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
         
                  
                   CheckAndRequestMicrophonePermission();  
                   // Set our view from the "main" layout resource  
                   SetContentView(Resource.Layout.activity_main);  
               }  
         
               public async Task<PermissionStatus> CheckAndRequestMicrophonePermission()  
               {  
                   var status = await Permissions.CheckStatusAsync<Permissions.Microphone>();  
         
                   if (status == PermissionStatus.Granted)  
                       return status;  
         
                    
         
                   if (Permissions.ShouldShowRationale<Permissions.Microphone>())  
                   {  
                       // Prompt the user with additional information as to why the permission is needed  
                   }  
         
                   status = await Permissions.RequestAsync<Permissions.Microphone>();  
         
                   return status;  
               }  
         
               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);  
               }  
           }  
       }  
    

    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.


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.