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

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

android.hardware.biometrics.BiometricPrompt.BiometricPrompt

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

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

  2. 添加权限


  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() {
						@Override
						public void onClick(DialogInterface dialogInterface, int i) {
							Toast.makeText(MainActivity.this, "取消验证", Toast.LENGTH_SHORT).show();
						}
					}).build();

			// 指纹识别回调
			BiometricPrompt.AuthenticationCallback authenticationCallback = new BiometricPrompt.AuthenticationCallback() {
				@Override
				public void onAuthenticationError(int errorCode, CharSequence errString) {
					super.onAuthenticationError(errorCode, errString);
					Log.i(TAG, "onAuthenticationError: errorCode = " + errorCode + ", errString = " + errString);
				}

				@Override
				public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
					super.onAuthenticationSucceeded(result);
					Log.i(TAG, "onAuthenticationSucceeded:");
				}

				@Override
				public void onAuthenticationFailed() {
					super.onAuthenticationFailed();
					Log.i(TAG, "onAuthenticationFailed:");
				}
			};

			// 开始验证指纹
            CancellationSignal cancellationSignal = new CancellationSignal();
			biometricPrompt.authenticate(cancellationSignal, getMainExecutor(), authenticationCallback);
		}
	}

效果
Android9.0指纹识别BiometricPrompt的简单使用_第1张图片

你可能感兴趣的:(android)