Solciitar ermiso permission of camera Xamarin Android

1

I'm trying to solve a problem, my App in xamarin needs the camera in a View. When the App is opened, it asks for the permissions (if the user grants the permissions, everything is fantastic), if the user rejects them, when entering the view, I put a button to request the permissions again, but it does not work. The famous poster does not appear of android requesting access. and another strange thing is that if I close the App I see the request for permission again -you only see the android message requesting permission, my app is no longer seen and is closed- Someone can guide me what may be happening

Button code:

    private void BtnSolicitarAccesoCamara_OnClick(object sender, System.EventArgs e)
    {
        Task.Factory.StartNew(async () =>
        {
            if (Plugin.Permissions.CrossPermissions.IsSupported)
            {
                var result = await Plugin.Permissions.CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Camera);

                if (result != Plugin.Permissions.Abstractions.PermissionStatus.Granted)

                {
                    var rp = await Plugin.Permissions.CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.Camera);

                }
            }
        });
    }

MainActivity

 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] global::Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);


        }
    
asked by Ariel Octavio D'Alfeo 11.09.2018 в 15:56
source

1 answer

1

in your Activity's OnCreate method: "JMontemagno"

Plugin.CurrentActivity.CrossCurrentActivity.Current.Init (this, bundle);

  private void BtnSolicitarAccesoCamara_OnClick(object sender, System.EventArgs e)
        {   
     try
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
            if (status != PermissionStatus.Granted)
            {
                if(await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Camera))
                {
                    await DisplayAlert("Need Camera", "Gunna need", "OK");
                }

                var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera);
                //Best practice to always check that the key exists
                if(results.ContainsKey(Permission.Camera))
                    status = results[Permission.Camera];
            }

            else if(status != PermissionStatus.Unknown)
            {
                await DisplayAlert("Camera Denied", "Can not continue, try again.", "OK");
            }
        }
        catch (Exception ex)
        {

            return ;
        }
    }
    
answered by 11.09.2018 / 17:31
source