'canAuthenticate()' 在 android 中已弃用

'canAuthenticate()' is deprecated in android

What should I use instead of biometricManager.canAuthenticate() 已弃用。

Doc

This method is deprecated. Use canAuthenticate(int) instead.

    BiometricManager biometricManager = BiometricManager.from(this);
    switch (biometricManager.canAuthenticate()){
        case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
            break;
        case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
            break;
        case BiometricManager.BIOMETRIC_SUCCESS:
            break;
        case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
            break;
        case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
            break;
    }

如何像上面那样使用canAuthenticate(int)

直接替换您的代码如下所示:

switch (canAuthenticate(Authenticators.BIOMETRIC_WEAK)) {
   case ...
}

根据 javadoc,可能的 return 值为 BIOMETRIC_SUCCESSBIOMETRIC_ERROR_HW_UNAVAILABLEBIOMETRIC_ERROR_NONE_ENROLLEDBIOMETRIC_ERROR_NO_HARDWAREBIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED., 和以前一样.

canAuthenticate() 的默认实现已弃用,因为它仅支持 Authenticators.BIOMETRIC_WEAK

使用以下新实现:

BiometricManager.from(context).canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK)

支持以下身份验证机制:

public interface Authenticators {
        /**
         * Any biometric (e.g. fingerprint, iris, or face) on the device that meets or exceeds the
         * requirements for <strong>Class 3</strong> (formerly <strong>Strong</strong>), as defined
         * by the Android CDD.
         */
        int BIOMETRIC_STRONG = 0x000F;

        /**
         * Any biometric (e.g. fingerprint, iris, or face) on the device that meets or exceeds the
         * requirements for <strong>Class 2</strong> (formerly <strong>Weak</strong>), as defined by
         * the Android CDD.
         *
         * <p>Note that this is a superset of {@link #BIOMETRIC_STRONG} and is defined such that
         * {@code BIOMETRIC_STRONG | BIOMETRIC_WEAK == BIOMETRIC_WEAK}.
         */
        int BIOMETRIC_WEAK = 0x00FF;

        /**
         * The non-biometric credential used to secure the device (i.e. PIN, pattern, or password).
         * This should typically only be used in combination with a biometric auth type, such as
         * {@link #BIOMETRIC_WEAK}.
         */
        int DEVICE_CREDENTIAL = 1 << 15;
    }