生物识别提示在某些设备上的Android 9和10上崩溃

时间:2019-09-25 00:49:17

标签: android android-biometric-prompt android-biometric

我正在使用BiometricPrompt让用户使用指纹身份验证登录到应用程序 我在PasswordActivity类中完成了以下操作:

     Executor executor = Executors.newSingleThreadExecutor();

    FragmentActivity activity = this;

    final BiometricPrompt biometricPrompt = new BiometricPrompt(activity, executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            if (errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
                // user clicked negative button
            } else {
                //TODO: Called when an unrecoverable error has been encountered and the operation is complete.
            }
        }

        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            //TODO: Called when a biometric is recognized.
            final String decryptedText = decryptText();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (decryptedText != null && !decryptedText.isEmpty()) {
                        editPassword.setText(decryptedText);
                        buttonNext();
                    }
                }
            });

        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            //TODO: Called when a biometric is valid but not recognized.
        }
    });

    final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("My App"))
            .setSubtitle("Log on into the app"))
            .setNegativeButtonText("Cancel").toUpperCase())
            .build();

    if (sharedPreferenceManager.isFingerprintEnabled(this))
        biometricPrompt.authenticate(promptInfo);   

这是我得到的例外。我必须设置吗?

setNegativeButton (CharSequence text, 
            Executor executor, 
            DialogInterface.OnClickListener listener) as well?

我正在使用此版本的实现'androidx.biometric:biometric:1.0.0-alpha03'。

Caused by java.lang.IllegalArgumentException: Executor must not be null
   at android.hardware.biometrics.BiometricPrompt$Builder.setNegativeButton + 182(BiometricPrompt.java:182)
   at androidx.biometric.BiometricFragment.onCreate + 201(BiometricFragment.java:201)
   at androidx.fragment.app.Fragment.performCreate + 2414(Fragment.java:2414)
   at androidx.fragment.app.FragmentManagerImpl.moveToState + 1418(FragmentManagerImpl.java:1418)
   at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState + 1784(FragmentManagerImpl.java:1784)
   at androidx.fragment.app.FragmentManagerImpl.moveToState + 1861(FragmentManagerImpl.java:1861)
   at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange + 3269(FragmentManagerImpl.java:3269)
   at androidx.fragment.app.FragmentManagerImpl.dispatchCreate + 3223(FragmentManagerImpl.java:3223)
   at androidx.fragment.app.FragmentController.dispatchCreate + 190(FragmentController.java:190)
   at androidx.fragment.app.FragmentActivity.onCreate + 369(FragmentActivity.java:369)
   at androidx.appcompat.app.AppCompatActivity.onCreate + 85(AppCompatActivity.java:85)

3 个答案:

答案 0 :(得分:0)

尝试更新依赖关系,当前最新版本已经是候选版本:

implementation "androidx.biometric:biometric:1.0.0-rc01"

答案 1 :(得分:0)

Caused by java.lang.IllegalArgumentException: Executor must not be null
   at android.hardware.biometrics.BiometricPrompt$Builder.setNegativeButton + 182(BiometricPrompt.java:182)

这表明您正在测试的设备上的框架没有从支持库接收执行程序(支持库中的错误),或者框架本身有错误。

您可以尝试使用更高版本的androidx.biometric库吗? Beta02是最近发布的,自alpha03以来,已经修复了许多问题。

另外,您要测试什么设备,如果它可以在Beta02上重现,您是否可以通过adb bugreport foo.zip获取错误报告,并将带有错误的示例应用程序附加到公开问题tracker上?

答案 2 :(得分:0)

您可以尝试将Executor executor = Executors.newSingleThreadExecutor();替换为:

private Handler handler = new Handler();

private Executor executor = new Executor() {
    @Override
    public void execute(Runnable command) {
        handler.post(command);
    }
};

这是根据this developer.android.com教程中给出的代码。