在我的应用程序中,我使用的是androidx生物识别支持库1.0.1。在Android 9上,我可以看到这种错误,但我不知道它们来自哪里:
02-01 13:00:06.422 23937 23937 E AndroidRuntime: FATAL EXCEPTION: main
02-01 13:00:06.422 23937 23937 E AndroidRuntime: Process: xxxxxxx, PID: 23871
02-01 13:00:06.422 23937 23937 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.hardware.fingerprint.FingerprintManager.getErrorString(int, int)' on a null object reference
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.hardware.biometrics.BiometricPrompt.lambda$sendError$0(BiometricPrompt.java:490)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.hardware.biometrics.-$$Lambda$BiometricPrompt$HqBGXtBUWNc-v8NoHYsj2gLfaRw.run(Unknown Source:6)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6672)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
法典:
KeyguardManager km = (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE);
BiometricPrompt.Builder builder = new BiometricPrompt.Builder(getContext().getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.setConfirmationRequired(true);
if (km != null && km.isDeviceSecure()) {
builder.setDeviceCredentialAllowed(true);
} else {
builder.setNegativeButton(getContext().getResources().getString(android.R.string.cancel), getContext().getApplicationContext().getMainExecutor(), (dialog, which) -> {
//something
});
}
} else {
builder.setNegativeButton(getContext().getResources().getString(android.R.string.cancel), getContext().getApplicationContext().getMainExecutor(), (dialog, which) -> {
//something
});
}
builder.setDescription(getContext().getResources().getString(R.string.confirm_pin));
builder.setTitle(getContext().getResources().getString(R.string.modify_pin));
BiometricPrompt prompt = builder.build();
prompt.authenticate(new CancellationSignal(), getContext().getMainExecutor(), new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
}
@Override
public void onAuthenticationFailed() {
}
});
> 生物识别支持库 1.0.1.在 Android 9 上,因为某些设备没有生物识别传感器。您可以按照以下步骤操作。
1> 检查是否支持生物识别
private fun checkBiometricSupportOrNot():Boolean {
val keyguardManager: KeyguardManager =
getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (!keyguardManager.isKeyguardSecure) {
notifyUser("Fingerprint hs not been enabled in settings.")
return false
}
if (ActivityCompat.checkSelfPermission(
this,
android.Manifest.permission.USE_BIOMETRIC
) != PackageManager.PERMISSION_GRANTED
) {
notifyUser("Fingerprint hs not been enabled in settings.")
return false
}
return if (packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) true else true
}
2>检查点击事件中的传导
tvLogin.setOnClickListener {
if (checkBiometricSupportOrNot()){
val biometricPrompt : BiometricPrompt = BiometricPrompt.Builder(this)
.setTitle("Title")
.setSubtitle("Authenticaion is required")
.setDescription("Fingerprint Authentication")
.setNegativeButton("Cancel", this.mainExecutor) { _, _ ->
}.build()
biometricPrompt.authenticate(getCancellationSignal(), mainExecutor, authenticationCallback)
}else{
notifyUser("Fingerprint hs not been enabled in settings.")
}
}
显然谷歌修复了生物识别库 1.1.0-beta01 版本中的错误。
修复了生物识别提示导致某些没有指纹传感器的 Android 9 设备崩溃的问题。(b/151443237(
注意:1.1.0 在 21 年 1 月 27 日稳定运行