简体   繁体   English

如何在低于Android 9.0的版本中使用Biometric Prompt API

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

I am trying to implement Biometric prompt API to authenticate user using fingerprint verification. 我正在尝试使用指纹验证来实现生物识别提示API以对用户进行身份验证。 I am successfully able to integrate Biometric prompt and it is working on andorid 9.0. 我成功地能够整合生物识别提示,它正在开发andorid 9.0。 But as the documentation suggests Biometric api is also backwards compatible, but when I build dialog using below code it shows API support warning. 但正如文档所示,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) 调用需要API级别28(当前最小值为15):new android.hardware.biometrics.BiometricPrompt.Builder less ...(Ctrl + F1)此检查扫描应用程序中的所有Android API调用,并警告任何调用不适用于此应用程序所针对的所有版本(根据清单中的最小SDK属性)

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();

What can I do to make this work on lower apis? 我可以做些什么来使这个工作在较低的apis? Here is Screenshot. 这是截图。 在此输入图像描述

Looks like Biometric Prompt API for older version is still in alpha. 看起来旧版本的Biometric Prompt API仍处于alpha状态。 If you are ok with an alpha version you can use this in build.gradle 如果您对alpha版本没问题,可以在build.gradle使用它

compile group: 'androidx.biometric', name: 'biometric', version: '1.0.0-alpha02'

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

There are only two versions listed here 此处仅列出两个版本

  • 1.0.0-alpha01 1.0.0-alpha01
  • 1.0.0-alpha02 1.0.0-alpha02

Source: https://mvnrepository.com/artifact/androidx.biometric/biometric 资料来源: https//mvnrepository.com/artifact/androidx.biometric/biometric

As per the library description, it says 根据图书馆的描述,它说

The Biometric library is a static library that you can add to your Android application. 生物识别库是一个静态库,您可以将其添加到Android应用程序中。 It invokes BiometricPrompt on devices running P and greater, and on older devices will show a compat dialog. 它在运行P和更高版本的设备上调用BiometricPrompt,在旧设备上将显示compat对话框。 Compatible on devices running API 14 or later. 兼容运行API 14或更高版本的设备。

Which would mean that all you need is this compat library and it would work on all version of Android. 这意味着你需要的只是这个compat库,它适用于所有版本的Android。 No need to keep two different version for above Android 9 and below Android 9. 无需为Android 9及以下Android 9保留两个不同的版本。

Here is my implementation for androidx.biometric , which Rohit5k2 suggested. 这是我对androidx.biometric的实现, Rohit5k2建议。 It's Kotlin, but i'm sure it won't be a problem. 这是Kotlin,但我相信这不会是个问题。 Hope this helps 希望这可以帮助

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)
    }
}

If you have an old android project ( even if its a hybrid android app ) follow these detailed steps : 如果您有一个旧的Android项目(即使它是一个混合Android应用程序),请按照以下详细步骤操作:

  1. So I had a Cordova android hybrid app old code . 所以我有一个Cordova android混合应用旧代码。 using Android studio 3.2 . 使用Android studio 3.2。 Build gradle version was very old , 2.8 and gradle version was 3.3. Build gradle版本很老,2.8和gradle版本是3.3。

  2. I first of all upgraded Android studio to latest version 3.3.2 我首先将Android工作室升级到最新版本3.3.2

  3. Now i decided to migrate the whole project to androidX. 现在我决定将整个项目迁移到androidX。 Remember it wont even let me do that with the previous version of Android studio, i dont know why. 请记住它甚至不会让我使用以前版本的Android工作室,我不知道为什么。

  4. When i clicked on Refactor -> Migrate to AndroidX. 当我点击Refactor - > Migrate to AndroidX时。 A pop up appeared saying "Upgrade the gradle version. So now I updated gradle version to 4.10.1 , it is still giving me error if i upgrade it to 5.2 ( i dont know why , I am still new to Android). Also updated build gradle to 3.3.2 一个弹出窗口出现了“升级gradle版本。所以现在我将gradle版本更新到4.10.1,如果我将它升级到5.2(我不知道为什么,我还是Android的新手),它仍然会给我错误。”同时更新建立gradle到3.3.2

5.My build.gradle (Module : App) looks like this : 5.我的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. Now App is syncing fine , Build ok. 现在App正在同步,构建正常。 I again tried Refactor -> Migrate to androidX. 我再次尝试了Refactor - >迁移到androidX。 This time Android studio started refactoring the code and provided me 70 code change suggestions . 这次Android工作室开始重构代码并为我提供了70个代码更改建议。

  2. These code changes are mainly the header file changes like : import "" . 这些代码更改主要是头文件更改,如:import“”。 So I opened this link - https://developer.android.com/jetpack/androidx/migrate and changed every import statement to the equal androidx statment. 所以我打开了这个链接 - https://developer.android.com/jetpack/androidx/migrate并将每个import语句更改为相同的androidx语句。

  3. After copy pasting all the changes I again compiled and synced the code . 复制粘贴所有更改后,我再次编译并同步代码。 after 3 resources and code compilation error , I was able to build the code . 在3个资源和代码编译错误之后,我能够构建代码。 This whole process took 1.2 hours . 整个过程耗时1.2小时。

  4. Finally i was able to import the biometric support API in build-extras.gradle (Module : app) , look at the file : 最后我能够在build-extras.gradle(Module: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. Finally , I was able to build the complete code and sync it . 最后,我能够构建完整的代码并进行同步。 So happy finally did it. 很高兴终于做到了。 Now i just have to use biometric API functions to integrate it into my code ( notice this code was written 3 years ago and given to me for integrating latest biometric API). 现在我只需要使用生物识别API函数将其集成到我的代码中(注意这段代码是3年前编写的,并提供给我用于集成最新的生物识别API)。

Yes I needed step by step answer like this one. 是的,我需要像这样一步一步地回答。

Still thanks to all who helped. 还要感谢所有帮助过的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM