Determine if biometric hardware is present and the user has enrolled biometrics on Android P

前端 未结 8 1169
半阙折子戏
半阙折子戏 2020-12-13 05:50

I\'m asked to show certain UI elements depending on the presence of biometric hardware. For Android 23-27 I use FingerprintManager#isHardwareDetected() and

8条回答
  •  醉梦人生
    2020-12-13 06:45

    The method - checks that the user has biometric authentication permission enabled for the app before using the package manager to verify that fingerprint authentication is available on the device. And even it will check if the user is enrolled or not.

    implementation 'androidx.biometric:biometric:1.0.0-alpha03'

    private Boolean checkBiometricSupport() {
    
        KeyguardManager keyguardManager =
                (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    
        PackageManager packageManager = this.getPackageManager();
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            notifyUser("This Android version does not support fingerprint authentication.");
            return false;
        }
    
        if(!packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT))
        {
            notifyUser("Fingerprint Sensor not supported");
            return false;
        }
    
        if (!keyguardManager.isKeyguardSecure()) {
            notifyUser("Lock screen security not enabled in Settings");
    
            return false;
        }
    
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.USE_BIOMETRIC) !=
                PackageManager.PERMISSION_GRANTED) {
            notifyUser("Fingerprint authentication permission not enabled");
    
            return false;
        }
    
        return true;
    }
    

提交回复
热议问题