可以参考android开发者 1.导入指纹依赖 //指纹依赖
api "androidx.biometric:biometric:1.1.0"
2.工具类
//我这里写了一个方法,也可以不写,直接把这个里面的代码放在上面的点击事件里也是可以的
private BiometricPrompt getprompt(){
Executor executor = ContextCompat.getMainExecutor(getContext());
BiometricPrompt.AuthenticationCallback callback=new BiometricPrompt.AuthenticationCallback() {
//指纹验证错误
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(getContext(),errString.toString(), Toast.LENGTH_SHORT).show();
}
//指纹验证成功
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getContext(), "指纹验证成功", Toast.LENGTH_SHORT).show();
}
//指纹验证失败
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getContext(),"指纹验证失败", Toast.LENGTH_SHORT).show();
}
};
BiometricPrompt biometricPrompt=new BiometricPrompt(this,executor,callback);
return biometricPrompt;
}
3.使用
//生物识别信息
BiometricPrompt.PromptInfo promptInfo=new BiometricPrompt.PromptInfo.Builder()
.setTitle("指纹登录")
.setDescription("user指纹")
.setNegativeButtonText("取消")
.build();
getprompt().authenticate(promptInfo);