Biometric
Biometric
是谷歌官方提供的生物识别验证类库,能调用包括目前Android设备上搭载的指纹、人脸、虹膜等系统级的生物认证。
要使用 Biometric
库添加生物识别身份验证,需要添加 Biometric
库的依赖项:
dependencies {
implementation 'androidx.biometric:biometric:1.0.0-beta01'
}
检查生物识别身份验证是否可用
检查生物识别是否可用:通过在 BiometricManager
类中使用 canAuthenticate()
方法,在调用 BiometricPrompt
之前检查设备是否支持生物识别身份验证。
BiometricManager biometricManager = BiometricManager.from(this);
switch (biometricManager.canAuthenticate()) {
case BiometricManager.BIOMETRIC_SUCCESS:
Log.d("应用可以进行生物识别技术进行身份验证。");
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Log.e("该设备上没有搭载可用的生物特征功能。");
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Log.e("生物识别功能当前不可用。");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
Log.e("用户没有录入生物识别数据。");
break;
}
弹出对话框进行生物验证
在需要弹出对话框的 Activity 或 Fragment 中,使用以下代码段中所示的逻辑来显示对话框:
private Handler handler = new Handler();
private Executor executor = new Executor() {
@Override
public void execute(Runnable command) {
handler.post(command);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
//点击按钮弹出生物验证
Button biometricLoginButton = findViewById(R.id.biometric_login);
biometricLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showBiometricPrompt();
}
});
}
//生物认证的setting
private void showBiometricPrompt() {
BiometricPrompt.PromptInfo promptInfo =
new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app") //设置大标题
.setSubtitle("Log in using your biometric credential") // 设置标题下的提示
.setNegativeButtonText("Cancel") //设置取消按钮
.build();
//需要提供的参数callback
BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this,
executor, new BiometricPrompt.AuthenticationCallback() {
//各种异常的回调
@Override
public void onAuthenticationError(int errorCode,
@NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(getApplicationContext(),
"Authentication error: " + errString, Toast.LENGTH_SHORT)
.show();
}
//认证成功的回调
@Override
public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
BiometricPrompt.CryptoObject authenticatedCryptoObject =
result.getCryptoObject();
// User has verified the signature, cipher, or message
// authentication code (MAC) associated with the crypto object,
// so you can use it in your app's crypto-driven workflows.
}
//认证失败的回调
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getApplicationContext(), "Authentication failed",
Toast.LENGTH_SHORT)
.show();
}
});
// 显示认证对话框
biometricPrompt.authenticate(promptInfo);
}
允许使用密码、图案、PIN码等非生物识别认证
设置PromptInfo
时调用setDeviceCredentialAllowed(true)
,允许利用设备 PIN 码、图案或密码进行身份验证。代码:
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setDeviceCredentialAllowed(true)
.build();
官方文档中同时使用了取消按钮 setDeviceCredentialAllowed(true)
和 setNegativeButtonText("Cancel")
非生物认证
实测会报错 Can't have both negative button behavior and device credential enabled
所以没有添加取消按钮,问题待定
搬运自Android开发者文档(可能需要科学上网)