导读 指纹登录可以实现应用的快捷登录,在Android6.0谷歌才提供统一指纹SDK接口,今天我们来介绍下指纹登录功能.

一、app里指纹登录介绍

指纹识别是Android6.0以上就开始支持的一个功能,并且类也不多,主要就是FingerprintManager还有它里面三个内部类(AuthenticationCallback、AuthenticationResult、CryptoObject);

指纹数据是在手机的设置里面,不是存到自己写的APP的;

指纹识别就只能识别,而不能在APP中录入指纹,想录入指纹可以,自己要到手机设置里面的指纹功能自己去添加,指纹识别功能能做的就是把用户放到感应区的指纹数据与手机设置里面的已录入的指纹数据进行比对,再执行成功失败的回调,仅此而已;

二、指纹登录步骤详解
1、申请权限

Android清单文件中的指纹的权限是:

uses-permission android:name="android.permission.USE_FINGERPRINT"
2、验证手机是否支持指纹

FingerprintManagerCompat提供了三个方法:

    isHardwareDetected() 判断是否有硬件支持
    isKeyguardSecure() 判断是否设置锁屏,因为一个手机最少要有两种登录方式
    hasEnrolledFingerprints() 判断系统中是否添加至少一个指纹
/** 
* 判断是否支持指纹识别 
*/ 
public static boolean supportFingerprint(Context mContext) { 
    if (Build.VERSION.SDK_INT < 23) { 
        Toast.makeText(mContext, "您的系统版本过低,不支持指纹功能", Toast.LENGTH_SHORT).show(); 
        return false; 
    } else { 
        KeyguardManager keyguardManager = mContext.getSystemService(KeyguardManager.class); 
        FingerprintManagerCompat fingerprintManager = FingerprintManagerCompat.from(mContext); 
        if (!fingerprintManager.isHardwareDetected()) { 
            Toast.makeText(mContext, "您的系统版本过低,不支持指纹功能", Toast.LENGTH_SHORT).show(); 
            return false; 
        } else if (keyguardManager != null && !keyguardManager.isKeyguardSecure()) { 
            Toast.makeText(mContext, "您的手机不支持指纹功能", Toast.LENGTH_SHORT).show(); 
            return false; 
        } else if (!fingerprintManager.hasEnrolledFingerprints()) { 
            Toast.makeText(mContext, "您至少需要在系统设置中添加一个指纹", Toast.LENGTH_SHORT).show(); 
            return false; 
        } 
    } 
    return true; 
} 
3、生成一个对称加密的key
val DEFAULT_KEY_NAME = "default_key" 
 lateinit var keyStore: KeyStore 
   private fun initKey() { 
        keyStore = KeyStore.getInstance("AndroidKeyStore") 
        keyStore.load(null) 
        val keyGenerator = 
            KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore") 
        val builder = KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME, 
            KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) 
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC) 
            .setUserAuthenticationRequired(true) 
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) 
        keyGenerator.init(builder.build()) 
        keyGenerator.generateKey() 
    } 
4、生成一个Cipher对象
private fun initCipher(mContext: Context?) { 
        val key = keyStore.getKey(DEFAULT_KEY_NAME, null) as SecretKey 
        val cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" 
                + KeyProperties.BLOCK_MODE_CBC + "/" 
                + KeyProperties.ENCRYPTION_PADDING_PKCS7) 
        cipher.init(Cipher.ENCRYPT_MODE, key) 
    } 
5、开启指纹验证
private void showFingerPrintDialog(Context context, Cipher cipher) { 
    FingerprintManagerCompat fingerprintManagerCompat = FingerprintManagerCompat.from(context); 
    FingerprintManagerCompat.CryptoObject cryptoObject = new FingerprintManagerCompat.CryptoObject(cipher); 
    CancellationSignal mCancellationSignal = new CancellationSignal(); 
    //识别过程中可以手动取消指纹识别 
    //mCancellationSignal.cancel(); 
    fingerprintManagerCompat.authenticate(cryptoObject, 0, mCancellationSignal, new MyCallBack(), null); 
} 
6、指纹验证回调
public class MyCallBack extends FingerprintManagerCompat.AuthenticationCallback { 
   @Override 
   public void onAuthenticationError(int errMsgId, CharSequence errString) { 
       //当出现错误的时候回调此函数,比如多次尝试都失败了的时候,errString是错误信息 
       //一般来说我们都是先判断一下是不是自己手动取消 
       Log.e("TAG", "errMsgId=" + errMsgId); 
       if (errMsgId == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT) { 
           Log.e("TAG", "" + errString); 
       } 
   } 
   //当指纹验证失败的时候会回调此函数,失败之后允许多次尝试,失败次数过多会停止响应一段时间然后再停止sensor的工作 
   @Override 
   public void onAuthenticationFailed() { 
       //指纹认证失败,请再试一次 
       Log.e("TAG", "onAuthenticationFailed"); 
   } 
   @Override 
   public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { 
      //错误时提示帮助,比如说指纹错误,我们将显示在界面上 让用户知道情况 
      Log.e("TAG", "helpString=" + helpString); 
   } 
   //当验证的指纹成功时会回调此函数,然后不再监听指纹sensor 
   @Override 
   public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { 
       //这里我们可以做取消弹框之类的 
       Log.e("TAG", "onAuthenticationSucceeded=" + result.toString()); 
   } 
} 

到此指纹验证登录流程完了,是不是很简单;

原文来自:

本文地址://gulass.cn/detailed-fingerprint-verification.html编辑:KSJXAXOAS,审核员:逄增宝

Linux大全:

Linux系统大全:

红帽认证RHCE考试心得: