Forum Xamarin.Android

Xamarin.Android and Plugin.Permissions

I have used the Plugin.Permissions with success in Xamarin.Forms Apps but in this case I have a simple Xamain.Android App which has recently stopped working as I was only requesting permissions via the manifest and I guess changing something has made this now work anymore (at least on some devices). Simple I thought - just add Plugin.Permissions to the app and it will just work.

Problem is it isn't. All of the suggestions I have seen relate to Xamarin.Forms and I can't get them to work under Xamarin.Android possibly because of where I am adding the code. In my Xamarin.Android App I have a splash screen which is set as the 'MainLauncher'. In OnCreate I have added the following lines :

        // Check permissions
        CrossCurrentActivity.Current.Init(this, bundle);
        CrossCurrentActivity.Current.Activity = this;

My CheckPermissions function is a direct copy of the many examples I have seen

            var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
            if (status != PermissionStatus.Granted)
                if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
                    await UserDialogs.Instance.AlertAsync("App requires permissions", "Permission Required");

            // Request any permissions which we do not currently have.
            var permissionsstatus = await CrossPermissions.Current.RequestPermissionsAsync(permissions.ToArray());
            foreach (var kvp in permissionsstatus)
                if (kvp.Value != PermissionStatus.Granted)
                    await UserDialogs.Instance.AlertAsync("One or more required permissions were not granted, cannot continue", "Permission Required");

The problem I am seeing is the call to RequestPermissionsAsync completes immediately without displaying any messages and returns an unknown status for all permissions requested. Other posts point to not initialising the permissions system correctly but the first 2 lines posted above do that so I'm not sure why it doesn't work...

Best Answer


Sign In or Register to comment.