API级别28中已弃用USE_FINGERPRINT。

50

常量USE_FINGERPRINT在API级别28中已被弃用,我们应该使用更通用的USE_BIOMETRIC,它也在同一API级别中被添加。

我在我的清单文件中交换了这些常量,但是当调用FingerprintManagerCompat.from(context).isHardwareDetected()时出现错误。

错误如下:

缺少必需的权限 - USE_FINGERPRINT

这是因为28.0.0-rc3的support v4库中FingerprintManagerCompat中有@RequiresPermission("android.permission.USE_FINGERPRINT")注释。

这是我可以忽略并继续使用新权限吗?


你在清单文件中添加了 <uses-permission android:name="android.permission.USE_FINGERPRINT"/> 权限吗? - AskNilesh
4
不,正如我所写的那样,我用新的 USE_BIOMETRIC 进行了更改。 - JerabekJakub
我认为你应该使用 BiometricPrompt 而不是使用 FingerprintManagerCompat - manuelwaldner
1
相关的 Google 追踪问题:https://issuetracker.google.com/issues/109826221。可悲的是,他们不打算修复它 :( - algrid
你只需要使用 BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS。不需要使用 FingerprintManagerCompat.from(context).isHardwareDetected()。 如果没有可用的硬件,它会返回 BIOMETRIC_ERROR_NO_HARDWARE,如果没有指纹(或面部、虹膜等)被注册,则会返回 BIOMETRIC_ERROR_NONE_ENROLLED - RiRomain
显示剩余3条评论
3个回答

130
我面临着同样的问题,在我看来,简短的答案是忽略弃用警告,只要你想在应用程序中支持指纹认证。
根据google dev blog所述,自API 28以来,谷歌推出了新的生物识别API,简化了整个生物识别认证过程。他们提供了一个用于身份验证对话框的简单构建器。此外,他们还支持面部和虹膜检测,如果你想支持它,可能只是时间问题,升级它可能会更值得。
到目前为止,我发现唯一的缺点是,如果您想检查例如指纹硬件是否可用,则必须启动身份验证过程以进行检查并等待错误回调。不过,废弃的指纹API提供了isHardwareDetected()hasEnrolledFingerprints()等方法来实现此目的。在这种情况下,如果您依赖此信息,则可能需要重新设计应用程序。那些方法被弃用的原因可能是它仅支持指纹,因此升级它并不是一个坏主意。
Google还为API 28以下的设备提供了compat 'androidx.biometric:biometric:1.0.0-alpha02'版本。似乎通过导入此依赖项,您可以简单地切换到USE_BIOMETRIC权限,而无需修改应用程序中的任何其他内容-您将不再受到警告的困扰。由于它仅处于alpha阶段,所以我会小心使用它。因此,只要您不使用生物识别API中的任何内容,您也可以简单地忽略该问题,并在您想要支持其他生物识别身份验证方法时再面对它。

编辑: 现在,兼容性库的beta版本已发布,'androidx.biometric:biometric:1.0.0-beta01'。更多信息请在此处查看。

现在,兼容性库的稳定版本于2019年12月18日发布,'androidx.biometric:biometric:1.0.1'。更多信息请点击此处


13
这是我们在Stack Overflow上更需要的高质量回答。+100 <3。可惜@JerabekJakub还没有选择你的答案作为官方答案,以给予你应得的积分。 - jungledev
@jungledev 为了辩护,SO没有将Mathew11的答案或您的评论通知我。直到8月16日有了新的回答后,我才收到通知,然后看到了这个答案和您的评论。 :-/ - JerabekJakub
现在可以使用"1.0.0-beta02"版本了,仍在等待最终版本。 - Sergio
2
在29版本中,有canAuthenticate函数 https://developer.android.com/reference/android/hardware/biometrics/BiometricManager#canAuthenticate() - Florian Walther

2

生物识别API提供了BiometricConstants来处理错误。

最初的回答
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
    super.onAuthenticationError(errorCode, errString)

    //The device does not have a biometric sensor.
    if (errorCode == BiometricPrompt.ERROR_HW_NOT_PRESENT){
      //Do something
    }
}

1

我知道现在已经有点晚了,但是如果有人想知道应该怎么做,请使用以下代码:

首先添加这个依赖项:

implementation 'androidx.biometric:biometric:1.1.0'

然后在您的登录活动中编写以下代码:

'BiometricManager biometricManager = BiometricManager.from(this);

// this Switch case is for detecting Finger print sensor and its situation
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK)) {
    case BiometricManager.BIOMETRIC_SUCCESS ->
            //this method is called when sensor exist
            Toast.makeText(this, "sensor found!", Toast.LENGTH_SHORT).show();

    case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
        //this method is called when sensor not found
            Toast.makeText(this, "no sensor found!", Toast.LENGTH_SHORT).show();

    case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
        //this method is called when sensor isn't available
            Toast.makeText(this, "not available", Toast.LENGTH_SHORT).show();

    case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED ->
        //this method is called when there is no saved finger print exist on your phone
            Toast.makeText(this, "There is no fingerprint saved on your phone!", Toast.LENGTH_SHORT).show();
}

Executor executor = ContextCompat.getMainExecutor(this);

BiometricPrompt biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
    @Override
    public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
        super.onAuthenticationError(errorCode, errString);

        //this method called when finger print not recognized by sensor after many attempts
        //sensor disabled for some seconds and can't be used
        // you have to wait for this error to clear automatically

        //YOUR CODE
    }

    @Override
    public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
        super.onAuthenticationSucceeded(result);

        //this method called when finger print detected successfully

        //YOUR CODE
    }

    @Override
    public void onAuthenticationFailed() {
        super.onAuthenticationFailed();

        //this method called when finger print not recognized by sensor

        //YOUR CODE
    }
});

// creating promptInfo panel
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Login")
        .setDescription("For Login put your finger on the sensor")
        //can use subtitle aswell
       // .setSubtitle()
        .setNegativeButtonText("cancel")
        .build();

//Let's say we have a Login button so ...
loginbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //This method authenticate your finger print
        biometricPrompt.authenticate(promptInfo);
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接