Raúl Cuadrado
Raúl Cuadrado

Reputation: 95

Unable to Override onRequestPermissionResult in Xamarin-Android

newbie in Xamarin here. I was unable to run my app because Camera was throwing an exception whenever I tried to open it for permission issues, so I followed the documentation about requesting permission in runtime, but it says that I have to override the onRequestPermissionResult method, but the compiler says:

The word @Override does not exist in the current context

As you can see here

If I use the override keyword between the method, I mean,

public override onRequestPermissionResult

I'm getting another error:

No suitable method found to override.

I don't know if it is a problem of inheritance, but I cannot make the class to inherit from anything more because it already inherits from PageRenderer class. Sorry if it's a dumb question but after a long research I couldn't figure out how to solve it.

Thanks in advance.

Upvotes: 0

Views: 584

Answers (2)

Billy Liu
Billy Liu

Reputation: 2168

You are using Xamarin.Forms right? If so, you need to use DependencyService and request for permission in Android project.

And it should be OnRequestPermissionsResult not onRequestPermissionResult

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Upvotes: 0

Tolulope
Tolulope

Reputation: 498

You can enable Camera Permission in your android manifest by adding this line:

<uses-permission android:name="android.permission.Camera" />

If you need it specifically at runtime:

string[] PermissionsCamera =
    {
                  Manifest.Permission.Camera

        };
             int RequestId = 0;
            if (this.CheckSelfPermission(PermissionsCamera[0]) == (int)Permission.Granted)
            {
                // permission already granted

            }

            else
            {
                // request permissions
                Activity.RequestPermissions(PermissionsCamera, RequestId);
            }
        }

To get feedback, you override OnRequestPermissionsResult:

public override  void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            switch (requestCode)
            {
                case RequestId:
                    {
                        if (grantResults[0] == Permission.Granted)
                        {
                            //Permission granted

                        }
                        else
                        {
                            //Permission Denied :

                        }
                    }
                    break;
            }
        }

Hope this helps

Upvotes: 1

Related Questions