'KeyguardService' doesn't exist in the actual context. I am unable to build a fingerprint auth

lokixlqntlokixlqnt Member ✭✭
edited October 16 in Xamarin.Android

Hello everyone,

I am trying to implement a fingerprint auth for an existing app at my work, I've been following the Microsoft tutorial to do so,

App is Xamarin.Forms, so I've created a helper class in the Xamarin.Android project to start implementing the fingerprint Auth

My code at the moment is this

class FingerprintHelper { public Context context; private void GetFingerPrint(Activity it) { FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.From(context); if (!fingerprintManager.IsHardwareDetected) { // On ne fait rien } string keyguardService = "keyguard"; KeyguardManager keyguardManager = (KeyguardManager)Context.GetSystemService(KeyguardService); if (!keyguardManager.IsKeyguardSecure) { } } }

I get an error when I try to call method GetSystemService with KeyguardService passed as a parameter. Error is : " 'KeyguardService' doesn't exist in the actual context. "
I already tried calling Context.KeyguardService,
I tried aswell to pass it as a string "keyguard" because on the sample project from Microsoft I've found the Android.Content namespace with all these const and Keyguard Service was part of it. But it didn't work neither.

I just feel like I'm missing something here. But I can't figure out what, even after many searches on internet.

I would be very glad if somebody helped me on this, thanks in advance.

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 16

    The context is any Android.Content.Context instance, typically the Activity. Try to create a static MainActivity instance in MainActivity. Then call the instance in FingerprintHelper class.

    Activity.cs

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        internal static MainActivity Instance { get; private set; }
        ...
    }
    

    FingerprintHelper.cs

    class FingerprintHelper
    {
        public Context context;
        private void GetFingerPrint(Activity it)
        {
            context = MainActivity.Instance;
            FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.From(context);
            if (!fingerprintManager.IsHardwareDetected)
            {
                // On ne fait rien 
            }
            string keyguardService = "keyguard";
            KeyguardManager keyguardManager = (KeyguardManager)Context.GetSystemService(KeyguardService);
    
            if (!keyguardManager.IsKeyguardSecure) { }
        }
    }
    
  • lokixlqntlokixlqnt Member ✭✭

    Thanks for your reply.

    Although it didn't solve the problem..
    In this line :
    KeyguardManager keyguardManager = (KeyguardManager)Context.GetSystemService(KeyguardService);
    KeyguardService is underlined, and when I check it with Visual Studio it says : KeyguardService doesn't exist in the actual context.

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to change Context.GetSystemService(KeyguardService) to GetSystemService(KeyguardService).

    KeyguardManager keyguardManager = (KeyguardManager)GetSystemService(KeyguardService);
    

    Tutorial about Finger Print Authentication:
    https://www.c-sharpcorner.com/article/xamarin-android-finger-print-authentication/

  • lokixlqntlokixlqnt Member ✭✭

    Yes I've been following this tutorial on top of the one in the Microsoft docs, that's what I initially wrote and when I write that I get the same error on both the method and the passed parameter. Both don't exist in the actual context, Visual Studio says.

  • lokixlqntlokixlqnt Member ✭✭

    Thanks a lot for your answer!!! It worked it out!

Sign In or Register to comment.