简体   繁体   English

使用设备凭据时 Android 生物识别无法导航

[英]Android Biometrics not navigating when using Device Credentials

I'm using Biometrics library to lock the app.我正在使用生物识别库来锁定应用程序。 Everything works fine and when I'm unlocking with fingerprint onAuthenticationSucceeded() get's called and device navigates from the lock screen.一切正常,当我使用指纹解锁时 onAuthenticationSucceeded() 被调用并且设备从锁定屏幕导航。 However if unlock with pattern the onAuthenticationSucceeded() get's called but navigation doesn't initialise and I'm left stuck on the lock screen fragment.但是,如果使用模式解锁 onAuthenticationSucceeded() 会被调用,但导航不会初始化,我会卡在锁定屏幕片段上。

EDIT: This only affects API29 with ANY device credentials编辑:这仅影响具有任何设备凭据的 API29

EDIT2: I'm also getting EDIT2:我也得到

FragmentNavigator: Ignoring popBackStack() call: FragmentManager has already saved its state FragmentNavigator:忽略 popBackStack() 调用:FragmentManager 已经保存了它的状态

FragmentNavigator: Ignoring navigate() call: FragmentManager has already saved its state FragmentNavigator:忽略导航()调用:FragmentManager 已经保存了它的状态

private lateinit var biometricPrompt: BiometricPrompt

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    biometricPrompt = createBiometricPrompt()
    return inflater.inflate(R.layout.lock_screen_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    val isAppLockEnabled: Boolean = PreferenceManager.getDefaultSharedPreferences(context)
        .getBoolean("lock_app_preference", false)

    // If app locks is not set go to home fragment else display app lock screen
    if (!isAppLockEnabled) {
        findNavController().navigate(R.id.action_lock_screen_fragment_dest_to_home_fragment_dest)
    } else {

   

        // Prompt appears when user clicks "Unlock".
        unlock_button.setOnClickListener {
            val promptInfo = createPromptInfo()
            biometricPrompt.authenticate(promptInfo)
        }
   }

}

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)
            Log.d("AuthenticationError()", "$errorCode :: $errString")
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            Log.d("AuthenticationFailed()", "Authentication failed for an unknown reason")
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            lock_icon.setImageResource(R.drawable.ic_unlock)
            lock_screen_text_view.text = getString(R.string.app_unlocked)
        //This doesn't work when using pattern unlock   findNavController().navigate(R.id.action_lock_screen_fragment_dest_to_home_fragment_dest)
        }
    }

    return BiometricPrompt(this, executor, callback)
}

private fun createPromptInfo(): BiometricPrompt.PromptInfo {
    return BiometricPrompt.PromptInfo.Builder()
        .setTitle("Unlock App")
        .setConfirmationRequired(false)
        .setDeviceCredentialAllowed(true)
        .build()
}

}

Ok, so I solved this issue.好的,所以我解决了这个问题。 Moved navigation from onAuthenticationSucceeded() to fragments onResume().将导航从 onAuthenticationSucceeded() 移动到 onResume() 片段。 Device credentials window pauses my app and somehow navigation cannot be called after that.设备凭据窗口暂停了我的应用程序,之后无法调用导航。

Solution code:解决方案代码:

private var isAppUnlocked : Boolean = false    


override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            isAppUnlocked = true
            unlockApp()
            Log.d("AuthenticationSuccess", "Authentication succeeded")
        }    

override fun onResume() {
    super.onResume()
    if(isAppUnlocked){
        unlockApp()
    }
}    

private fun unlockApp(){
    lock_icon.setImageResource(R.drawable.ic_unlock)
    lock_screen_text_view.text = getString(R.string.app_unlocked)
    findNavController().navigate(R.id.action_lock_screen_fragment_dest_to_home_fragment_dest)
}    

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

相关问题 您能否确定用户是否刚刚使用生物识别技术解锁了移动设备? - Can you determine if the user just unlocked the mobile device using biometrics? 指纹上的 Android 生物识别 UserNotAuthenticatedException - Android biometrics UserNotAuthenticatedException on fingerprint 使用在PHP中连接的生物识别技术创建Android Attendace Monitoring应用程序 - Creating an Android Attendace Monitoring application using biometrics that connected in PHP Android biometrics api 不使用人脸识别来解锁 - Android biometrics api isn't using face recognition to unlock Android生物识别登录 - Android biometrics login 使用Android设备凭据访问应用程序的关键部分 - Using Android device credentials to access critical part of the app 列出 Android 中注册的生物识别(和类型) - List the enrolled biometrics (and types) in Android 导航时Android应用程序停止 - Android app stops when navigating 摄像头访问被阻止时的生物识别提示(面部验证)(Android 12 - Pixel) - Biometrics prompt (face authentication) when camera access is blocked (Android 12 - Pixel) 在Android中使用指纹身份验证时存储用户凭据的位置 - Where to store user credentials when using fingerprint authentication in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM