简体   繁体   中英

Why my android xamarin app close automatically when permission is denied?

I am developing a mobile app with Xamarin. I want to access the camera to take a picture so as usual android asks for permission on it. When user click "allow", it works fine, but when the user clicks deny, the app closes automatically. I have tried to debug, and the error when the user clicks on Deny is UnauthorizedAccessException. I understand all the permission handling is in MainActivity but how can I avoid this happening anymore?

As you mentioned, you need to handle with a try/catch block into OnRequestPermissionsResult method.

public class MainActivity : FormsAppCompatActivity
    {
        ...

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            try
            {
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                ((PermissionsService)PermissionsService.Instance).OnRequestPermissionResult(requestCode, permissions, grantResults);
            }
            catch(UnauthorizedAccessException)
            {
                HandleExceptionAndContinue()
            }
        }
    }

check the permission in your main activity before you go.

if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == (int)Permission.Granted)
{
    // We have permission, go ahead and use the camera.
}
else
{
    // We have not got permission, can't use camera
}

I have tried to debug, and the error when the user clicks on Deny is UnauthorizedAccessException. I understand all the permission handling is in MainActivity but how can I avoid this happening anymore?

According to your description, I guess that your project is Xamarin.Forms, and you want to access camera, now, you don't want to get UnauthorizedAccessException when you choose Deny for Camera? AM I right?

If yes, I suggest you can use Plugin.Permissions by Nuget package installing firstly.

Then in ContentPage, you can use the following code to request camera permission.

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 that camera", "OK");
                }

                var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera);
                status = results[Permission.Camera];
            }

            if (status == PermissionStatus.Granted)
            {
                 Console.WriteLine("camera access");
            }
            else if (status != PermissionStatus.Unknown)
            {
                await DisplayAlert("camera Denied", "Can not continue, try again.", "OK");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex);
            LabelGeolocation.Text = "Error: " + ex;
        }

Then add some code in MainActivity.cs

 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 add the following code in Mainactivity.cs OnCreate method:

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

About using for other permissions, you can take a look at:

https://github.com/jamesmontemagno/PermissionsPlugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM