首页 > 解决方案 > 使用生物识别 androidX 库 1.1.0 崩溃

问题描述

在我的应用程序中,我使用的是 androidX 生物识别支持库 1.1.0。在所有 Android 版本上,我的应用程序都崩溃了。这是我使用的代码片段

例外:

java.lang.IllegalStateException:必须从片段主机的主线程调用

在:

public boolean isFingerprintAuthAvailable(Context mContext) {
        BiometricManager biometricManager = BiometricManager.from(mContext);
        if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE) {
            return false;
        } else if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
            return false;
        } else if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED) {
            return false;
        } else if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
            return true;
        }
        return false;
    }

BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                    .setTitle("fingerPrintTitle")
                    .setDescription("Description")
                    .setNegativeButtonText(""negative text")
                    .build();

 if (isFingerprintAuthAvailable(mContext)) {
        mBiometricPrompt.authenticate(promptInfo, cryptoObject);
 }

标签: androidandroid-fragmentsandroid-fingerprint-apiandroid-biometric-promptandroid-biometric

解决方案


我已将代码移至 UI 线程

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBiometricPrompt.authenticate(promptInfo, cryptoObject);
            }
        });

推荐阅读