首页 > 解决方案 > Android BiometricPrompt on AuthenicationSucceeded 不起作用

问题描述

Android BiometricPrompt 已完成指纹识别,但 onAuthenicationSucceeded 不起作用。

我用了androidx

示例:Log.i("finger", "onAuthenticationSucceeded")不输出

复制此代码:https ://developer.android.com/training/sign-in/biometric-auth#kotlin

主要活动:

private fun showBiometricPrompt() {
    val promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Cancel")
        .build()
    Log.i("finger", "o")
    val biometricPrompt = BiometricPrompt(this, executor,
        object : BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationError(errorCode: Int,
                                               errString: CharSequence) {
                Log.i("finger", "onAuthenticationError")
                super.onAuthenticationError(errorCode, errString)
                Toast.makeText(applicationContext,
                    "Authentication error: $errString", Toast.LENGTH_SHORT)
                    .show()
            }

            override fun onAuthenticationSucceeded(
                result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                Log.i("finger", "onAuthenticationSucceeded")
                val authenticatedCryptoObject: BiometricPrompt.CryptoObject? =
                    result.getCryptoObject()
                // User has verified the signature, cipher, or message
                // authentication code (MAC) associated with the crypto object,
                // so you can use it in your app's crypto-driven workflows.

            }

            override fun onAuthenticationFailed() {
                Log.i("finger", "onAuthenticationFailed")
                super.onAuthenticationFailed()
                Toast.makeText(applicationContext, "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show()
            }
        })

    // Displays the "log in" prompt.
    biometricPrompt.authenticate(promptInfo)
    }
}

来自当前 android studio 的日志

2019-11-27 19:45:22.348 11633-11633/? I/e.myapplicatio: Late-enabling -Xcheck:jni
2019-11-27 19:45:23.124 11633-11633/com.example.myapplication W/e.myapplicatio: JIT profile information will not be recorded: profile file does not exits.
2019-11-27 19:45:23.127 11633-11633/com.example.myapplication I/chatty: uid=10263(com.example.myapplication) identical 10 lines
2019-11-27 19:45:23.127 11633-11633/com.example.myapplication W/e.myapplicatio: JIT profile information will not be recorded: profile file does not exits.
2019-11-27 19:45:23.197 11633-11633/com.example.myapplication I/InstantRun: starting instant run server: is main process
2019-11-27 19:45:23.363 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2019-11-27 19:45:23.363 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2019-11-27 19:45:23.363 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2019-11-27 19:45:23.363 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2019-11-27 19:45:23.363 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2019-11-27 19:45:23.421 11633-11633/com.example.myapplication D/PhoneWindow: windowLightStatusBar : false, disable View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
2019-11-27 19:45:23.489 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking)
2019-11-27 19:45:23.593 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-11-27 19:45:23.594 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-11-27 19:45:23.610 11633-11633/com.example.myapplication W/e.myapplicatio: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2019-11-27 19:45:23.726 11633-11633/com.example.myapplication I/App can authenticate using biometrics.: OK
2019-11-27 19:45:23.760 11633-11633/com.example.myapplication D/OpenGLRenderer: Skia GL Pipeline
2019-11-27 19:45:23.835 11633-11633/com.example.myapplication I/TouchFilterInputStageHelper: [needTouchEventFilter] Displayid : 0
2019-11-27 19:45:23.841 11633-11633/com.example.myapplication D/BezellessGripSuppressionFilter: getdisplaysize, x : 1440 y : 2880
2019-11-27 19:45:23.924 11633-11733/com.example.myapplication I/Adreno: QUALCOMM build                   : 6aca7ed, I9a84ac9597
    Build Date                       : 08/23/19
    OpenGL ES Shader Compiler Version: EV031.25.03.07
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.7.5.2.R1.09.00.00.538.032
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2019-11-27 19:45:23.924 11633-11733/com.example.myapplication I/Adreno: Build Config                     : S L 6.0.7 AArch64
2019-11-27 19:45:23.924 11633-11733/com.example.myapplication D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8996.so from current namespace instead of sphal namespace.
2019-11-27 19:45:23.938 11633-11733/com.example.myapplication I/Adreno: PFP: 0x005ff112, ME: 0x005ff066
2019-11-27 19:45:23.980 11633-11733/com.example.myapplication I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-11-27 19:45:23.982 11633-11733/com.example.myapplication I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2019-11-27 19:45:23.983 11633-11733/com.example.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
2019-11-27 19:45:23.983 11633-11733/com.example.myapplication D/OpenGLRenderer: Swap behavior 2
2019-11-27 19:45:24.008 11633-11733/com.example.myapplication D/vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
2019-11-27 19:45:24.009 11633-11733/com.example.myapplication D/vndksupport: Loading /vendor/lib64/hw/gralloc.msm8996.so from current namespace instead of sphal namespace.
2019-11-27 19:45:24.178 11633-11633/com.example.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@64768ba time:68742526
2019-11-27 19:45:26.690 11633-11633/com.example.myapplication I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
2019-11-27 19:45:26.761 11633-11633/com.example.myapplication I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP

标签: androidkotlinandroid-biometric-promptandroid-biometric

解决方案


尝试按照此博客文章中的说明进行操作。它提供了有关如何实现 BiometricPrompt 的分步说明。此博客文章中还有更多详细信息。

这是一个摘要

1-将 Gradle 依赖项添加到您的应用程序模块(最新版本

 def biometric_version= '1.0.0'
 implementation "androidx.biometric:biometric:$biometric_version"

2- 创建一个 BiometricPrompt 的实例。您应该在onCreate()您的活动或onCreateView()片段中获取此值

private fun createBiometricPrompt(): BiometricPrompt {
   val executor = ContextCompat.getmainExecutor(context)

   val callback = object: BiometricPrompt.AuthenticationCallback() {
       override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
           super.onAuthenticationError(errorCode, errString)
           showMessage("$errorCode :: $errString")
       }

       override fun onAuthenticationFailed() {
           super.onAuthenticationFailed()
           showMessage("Authentication failed for an unknown reason")
       }

       override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
           super.onAuthenticationSucceeded(result)
           showMessage("Authentication was successful")
       }
   }

   val biometricPrompt = BiometricPrompt(context, executor, callback)
   return biometricPrompt
}

3- 构建 PromptInfo 对象

private fun createPromptInfo(): BiometricPrompt.PromptInfo {
   val promptInfo = BiometricPrompt.PromptInfo.Builder()
       .setTitle("My App's Authentication")
       .setSubtitle("Please login to get access")
       .setDescription("My App is using Android biometric authentication")
              .setDeviceCredentialAllowed(true)
       .build()
   return promptInfo
}

4-要求用户进行身份验证

val canAuthenticate = biometricManager.canAuthenticate()
if (canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS) {

   biometricPrompt.authenticate(createPromptInfo())
} else {
   Log.d(TAG, "could not authenticate because: $canAuthenticate")
}

推荐阅读