Android 9.0以下版本如何使用Biometric Prompt API

How to use Biometric Prompt API in versions lower than Android 9.0

我正在尝试实施生物识别提示 API 以使用指纹验证对用户进行身份验证。我成功地集成了生物识别提示,并且它正在使用 andorid 9.0。但是正如文档所建议的那样,Biometric api 也是向后兼容的,但是当我使用下面的代码构建对话框时,它会显示 API 支持警告。

Call requires API level 28 (current min is 15): new android.hardware.biometrics.BiometricPrompt.Builder less... (Ctrl+F1) This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest)

mBiometricPrompt = new BiometricPrompt.Builder(this)
                        .setDescription("Description")
                        .setTitle("Title")
                        .setSubtitle("Subtitle")
                        .setNegativeButton("Cancel", getMainExecutor(), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.i(TAG, "Cancel button clicked");
                            }
                        })
                        .build();

我该怎么做才能在较低的 apis 上完成这项工作?这是截图。

看起来旧版本的生物识别提示 API 仍处于 alpha 阶段。如果您对 alpha 版本没问题,您可以在 build.gradle

中使用它
compile group: 'androidx.biometric', name: 'biometric', version: '1.0.0-alpha02'

来源:https://mvnrepository.com/artifact/androidx.biometric/biometric/1.0.0-alpha02

这里只列出了两个版本

  • 1.0.0-alpha01
  • 1.0.0-alpha02

来源:https://mvnrepository.com/artifact/androidx.biometric/biometric

根据图书馆的描述,它说

The Biometric library is a static library that you can add to your Android application. It invokes BiometricPrompt on devices running P and greater, and on older devices will show a compat dialog. Compatible on devices running API 14 or later.

这意味着您所需要的只是这个兼容库,它适用于 Android 的所有版本。 Android 9 以上和 Android 9 以下

无需保留两个不同的版本

这是我对 androidx.biometric 的实施,Rohit5k2 建议。它是 Kotlin,但我相信这不会成为问题。希望这有帮助

fun FragmentActivity.auth(successCallback: () -> Unit, cancelSignal: CancellationSignal = CancellationSignal()) {
    if (Build.VERSION.SDK_INT < 23) {
        successCallback()
        return
    }

    val biometricPrompt = BiometricPrompt(this, MainThreadExecutor(), object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            successCallback()
        }

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            if (errorCode == ERROR_NEGATIVE_BUTTON) {
                cancelSignal.cancel()
            }
        }
    })

    val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle(getString(R.string.title))
            .setSubtitle(getString(R.string.auth))
            .setDescription(getString(R.string.biometric_desc))
            .setNegativeButtonText(getString(R.string.biometric_negative))
            .build()

    biometricPrompt.authenticate(promptInfo)
}

class MainThreadExecutor : Executor {
    private val handler = Handler(Looper.getMainLooper())

    override fun execute(r: Runnable) {
        handler.post(r)
    }
}

如果您有一个旧的 android 项目(即使它是一个混合 android 应用程序),请按照以下详细步骤操作:

  1. 所以我有一个 Cordova android 混合应用程序旧代码。使用 Android 工作室 3.2。构建 gradle 版本很旧,2.8 和 gradle 版本是 3.3。

  2. 我首先升级了Android studio 到最新版本3.3.2

  3. 现在我决定将整个项目迁移到 androidX。请记住,它甚至不允许我使用以前版本的 Android studio 执行此操作,我不知道为什么。

  4. 当我单击重构 -> 迁移到 AndroidX 时。出现一个弹出窗口说“升级 gradle 版本。所以现在我将 gradle 版本更新到 4.10.1,如果我将它升级到 5.2,它仍然给我错误(我不知道为什么,我是Android 仍然是新手。还将构建 gradle 更新为 3.3.2

5.My build.gradle(模块:App)看起来像这样:

apply plugin: 'com.android.application'

buildscript {
    repositories {
        jcenter{ url "http://jcenter.bintray.com/" }
        google()
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "com.google.gms:google-services:3.0.0" //FCM Config

    }
   }
  1. 现在应用程序同步正常,构建正常。我再次尝试重构 -> 迁移到 androidX。这次Androidstudio开始重构代码,给我提供了70条改码建议。

  2. 这些代码更改主要是头文件更改,如:import ""。所以我打开这个 link - https://developer.android.com/jetpack/androidx/migrate 并将每个导入语句更改为相等的 androidx 语句。

  3. 复制粘贴所有更改后,我再次编译并同步了代码。在 3 个资源和代码编译错误之后,我能够构建代码。整个过程耗时1.2小时.

  4. 我终于能够在 build-extras.gradle(模块:app)中导入生物识别支持 ​​API,查看文件:

        dependencies {
            api 'androidx.appcompat:appcompat:1.0.2'
            api "com.squareup.picasso:picasso:2.4.0"
            api "com.google.android.material:material:1.1.0-alpha04"
            api "com.google.firebase:firebase-messaging:9.2.0" //FCM Config
            api 'com.rmtheis:tess-two:6.0.2'
            api 'com.github.bumptech.glide:glide:3.8.0'
            api 'androidx.legacy:legacy-support-v4:1.0.0'
    
            api "androidx.biometric:biometric:1.0.0-alpha03"
        }
    }
    
  5. 最后,我能够构建完整的代码并同步它。很高兴终于做到了。现在我只需要使用生物识别 API 函数将其集成到我的代码中(请注意,这段代码是 3 年前编写的,是为了集成最新的生物识别 API 而提供给我的)。

是的,我需要像这样的一步步回答。

还是感谢所有帮助过的人。