代码之家  ›  专栏  ›  技术社区  ›  Juan Cruz Soler

生物识别PrompCompat丢失

  •  3
  • Juan Cruz Soler  · 技术社区  · 6 年前

    Android P引入了 Biometrics API .
    现在我们应该使用 BiometricPrompt 类以在我们的应用程序中集成生物识别身份验证( FingerprintManager 被贬低)。

    问题是这个类只在API 28上可用。

    Biometrics documentation 说:

    还为运行Android O和更早版本的设备提供了支持库,允许应用程序在更多设备上利用此API的优势。

    但我找不到支持库。
    它存在吗?还是将在未来的实现中添加?

    2 回复  |  直到 6 年前
        1
  •  7
  •   JayShortway    6 年前

    我想在你提问的时候 失踪。 1.0.0-alpha01 于9月发布。最新版本为 1.0.0-alpha02 .
    将此添加到build.gradle:

    implementation 'androidx.biometric:biometric:1.0.0-alpha02'
    

    ( Maven Repository )

        2
  •  4
  •   Juan Cruz Soler    6 年前

    正如@jayshortway所回答的,向后兼容的依赖关系是:

    implementation 'androidx.biometric:biometric:1.0.0-alpha02'
    

    但是实现不同于 android.hardware.biometrics.BiometricPrompt 实施:

    class BiometricsManagerImpl {
    
        private val executor = MainThreadExecutor()
    
        override fun authenticate(activity: FragmentActivity) {
            val prompt = BiometricPrompt(
                activity,
                executor,
                object : BiometricPrompt.AuthenticationCallback() {
                    override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {                        
                        super.onAuthenticationError(errorCode, errString)
                        // Handle authentication errors
                    }
    
                    override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                        super.onAuthenticationSucceeded(result)
                        // Authentication Succeeded
                    }
                })
    
            val promptInfo = BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric Authentication")
                .setDescription("Please authenticate in order to verify your identity")
                .setNegativeButtonText("Cancel")
                .build()
    
            prompt.authenticate(promptInfo)
        }
    
        inner class MainThreadExecutor : Executor {
            private val handler = Handler(Looper.getMainLooper())
    
            override fun execute(runnable: Runnable) {
                handler.post(runnable)
            }
        }
    }