当前位置: 代码迷 >> 综合 >> Android9.0指纹识别BiometricPrompt的简单使用
  详细解决方案

Android9.0指纹识别BiometricPrompt的简单使用

热度:27   发布时间:2024-02-11 22:06:01.0

谷歌在Android 9.0 API版本28,发布了生物信息综合验证的基础 API

android.hardware.biometrics.BiometricPrompt.BiometricPrompt

所以在android9.0之后使用系统提供的BiometricPrompt来进行指纹识别,BiometricPrompt提供了对话框的构建,方便使用。

  1. 首先项目的sdk版本要在28以上

  2. 添加权限

<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
  1. 代码
private void checkFingerprint() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {// 构建对话框BiometricPrompt biometricPrompt = new BiometricPrompt.Builder(this).setTitle("指纹验证").setDescription("请验证指纹").setNegativeButton("取消", getMainExecutor(), new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {Toast.makeText(MainActivity.this, "取消验证", Toast.LENGTH_SHORT).show();}}).build();// 指纹识别回调BiometricPrompt.AuthenticationCallback authenticationCallback = new BiometricPrompt.AuthenticationCallback() {@Overridepublic void onAuthenticationError(int errorCode, CharSequence errString) {super.onAuthenticationError(errorCode, errString);Log.i(TAG, "onAuthenticationError: errorCode = " + errorCode + ", errString = " + errString);}@Overridepublic void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {super.onAuthenticationSucceeded(result);Log.i(TAG, "onAuthenticationSucceeded:");}@Overridepublic void onAuthenticationFailed() {super.onAuthenticationFailed();Log.i(TAG, "onAuthenticationFailed:");}};// 开始验证指纹CancellationSignal cancellationSignal = new CancellationSignal();biometricPrompt.authenticate(cancellationSignal, getMainExecutor(), authenticationCallback);}}

效果