0

In my app, I'm using androidX biometric support library 1.1.0. On all Android versions, my app is crashing. Here is the code snippet that I have used

Exception:

java.lang.IllegalStateException: Must be called from main thread of fragment host

In:

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);
 }
Zoe
  • 23,712
  • 16
  • 99
  • 132
Sandeep
  • 11
  • 2
  • 1
    Have you read the error message? – Martin Zeitler Mar 12 '21 at 19:26
  • Yeah I did. The crash is throwing from FragmentManager class. – Sandeep Mar 15 '21 at 18:22
  • The code you've posted is useless, because it is unknown where it runs... as the error message hints for. – Martin Zeitler Mar 15 '21 at 20:46
  • @MartinZeitler Thank you for your reply. I got the answer. I was calling the authenticate() method on Webview. And Webview using JavascriptInterface to post the message. I moved the mBiometricPrompt.authenticate(promptInfo, cryptoObject); to UI thread using runOnUiThread https://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview – Sandeep Mar 16 '21 at 14:53

1 Answers1

0

I have moved the code to UI thread

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