簡體   English   中英

如何在低於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支持警告。

調用需要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();

我可以做些什么來使這個工作在較低的apis? 這是截圖。 在此輸入圖像描述

看起來舊版本的Biometric Prompt 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

根據圖書館的描述,它說

生物識別庫是一個靜態庫,您可以將其添加到Android應用程序中。 它在運行P和更高版本的設備上調用BiometricPrompt,在舊設備上將顯示compat對話框。 兼容運行API 14或更高版本的設備。

這意味着你需要的只是這個compat庫,它適用於所有版本的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 studio 3.2。 Build gradle版本很老,2.8和gradle版本是3.3。

  2. 我首先將Android工作室升級到最新版本3.3.2

  3. 現在我決定將整個項目遷移到androidX。 請記住它甚至不會讓我使用以前版本的Android工作室,我不知道為什么。

  4. 當我點擊Refactor - > Migrate to AndroidX時。 一個彈出窗口出現了“升級gradle版本。所以現在我將gradle版本更新到4.10.1,如果我將它升級到5.2(我不知道為什么,我還是Android的新手),它仍然會給我錯誤。”同時更新建立gradle到3.3.2

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. 現在App正在同步,構建正常。 我再次嘗試了Refactor - >遷移到androidX。 這次Android工作室開始重構代碼並為我提供了70個代碼更改建議。

  2. 這些代碼更改主要是頭文件更改,如:import“”。 所以我打開了這個鏈接 - https://developer.android.com/jetpack/androidx/migrate並將每個import語句更改為相同的androidx語句。

  3. 復制粘貼所有更改后,我再次編譯並同步代碼。 在3個資源和代碼編譯錯誤之后,我能夠構建代碼。 整個過程耗時1.2小時。

  4. 最后我能夠在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. 最后,我能夠構建完整的代碼並進行同步。 很高興終於做到了。 現在我只需要使用生物識別API函數將其集成到我的代碼中(注意這段代碼是3年前編寫的,並提供給我用於集成最新的生物識別API)。

是的,我需要像這樣一步一步地回答。

還要感謝所有幫助過的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM