Xamarin permissions plugin RequestPermissionsAsync not returning

ⅰ亾dé卋堺 提交于 2020-06-15 01:15:17

问题


I've tried opening tickets for this in the creators github account but he just kept closing it without confirmation that the issue was resolved.

I'm trying to implement this plugin for xamarin forms permissions and everything works well except for one thing, when I try to request for permission using a real device, the task never returns whether or not the user allowed or denied the request. When I restart the project I can see that the user's last action regarding the permission took effect, however at the moment of the request, the function just hangs and I can't make decisions based on what the user selected. Below is a code snippet

var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
                if (status != PermissionStatus.Granted)
                {
                    //This line never returns
                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
                    status = results[Permission.Location];
                }

Perhaps anyone here can shed any light? Any help would be appreciated. Thank you.


回答1:


I've got this issue for sometime and fixed it. You can follow these steps to insure a perfect installation.

In NuGet Solution Manager :-

  1. Install NuGet Plugin.CurrentActivity for Android.
  2. Install NuGet Xamarin.Android.SupportAnnotations for Android.
  3. Install NUGet Plugin.Permissions for all projects.

In Android MainActivity.cs :-

  1. Add this line CrossCurrentActivity.Current.Activity = this; after the line base.OnCreate(bundle/savedInstanceState); under the protected override void OnCreate method.

  2. Add this method -Or overwrite it if exists:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
  1. Resolve all the missing namespaces.

By now you should have a perfect installation, go and retry your code, it should work now.




回答2:


Aside of specifying the permission in your Android manifest and initializing the CurrentActivity, don't forget to override the OnRequestPermissionsResult method as well in your MainActivity so that the permission results are passed to the PermissionsImplementation, as described on the plugin page:

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
    {
        Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);    
    }


来源:https://stackoverflow.com/questions/50338025/xamarin-permissions-plugin-requestpermissionsasync-not-returning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!