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

2023-05-16

谷歌在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() {
						@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);
		}
	}

效果

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android9.0指纹识别BiometricPrompt的简单使用 的相关文章

随机推荐