一.使用第三方代码
1.第一版
依赖:implementation “androidx.biometric:biometric:1.1.0”
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("指纹验证")
.setDescription("正在指纹验证中")
.setNegativeButtonText("取消")
.build();
new BiometricPrompt(this, ContextCompat.getMainExecutor(this), new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
ARouter.getInstance().build("/huanxin/HaunXinMainActivity").navigation();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
})
.authenticate(promptInfo);
2.第二版:判断
public class Main6Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
Toast.makeText(this, "当前手机不支持指纹解锁", Toast.LENGTH_SHORT).show();
return;
}
FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
if(!fingerprintManager.isHardwareDetected()){
Toast.makeText(this, "当前手机不支持指纹感应区", Toast.LENGTH_SHORT).show();
return;
}
if(!fingerprintManager.hasEnrolledFingerprints()){
Toast.makeText(this, "当前手机没有录入指纹", Toast.LENGTH_SHORT).show();
return;
}
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("指纹验证")
.setNegativeButtonText("取消")
.build();
new BiometricPrompt(this, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(Main6Activity.this, "校验失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(Main6Activity.this, "校验成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
})
.authenticate(promptInfo);
}
}
二.原生代码
权限:
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
代码:
package com.bawei.day9day10;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main5Activity extends AppCompatActivity {
private FingerprintManager manager;
private KeyStore keyStore;
private static final String DEFAULT_KEY_NAME = "default_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
manager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
if (isSupportFingerPrint()) {
initKey();
initCipher();
}
}
private boolean isSupportFingerPrint() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
return false;
}
if (!manager.isHardwareDetected()) {
Toast.makeText(this, "没有指纹识别模块", Toast.LENGTH_SHORT).show();
return false;
}
if (!manager.hasEnrolledFingerprints()) {
Toast.makeText(this, "您?少需要在系统设置中添加?个指纹", Toast.LENGTH_SHORT).show();
return false;
}
} else {
Toast.makeText(this, "您的?机暂不?持指纹识别", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private void initKey() {
try {
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
keyGenerator.init(builder.build());
keyGenerator.generateKey();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void initCipher() {
try {
SecretKey key = (SecretKey) keyStore.getKey(DEFAULT_KEY_NAME, null);
Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
cipher.init(Cipher.ENCRYPT_MODE, key);
showFingerPrintDialog(cipher);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void showFingerPrintDialog(Cipher cipher) {
CancellationSignal cancel = new CancellationSignal();
manager.authenticate(new FingerprintManager.CryptoObject(cipher), cancel, 0, new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
Toast.makeText(Main5Activity.this, "ok", Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationFailed() {
}
}, null);
}
}
|