Preface指纹登录可以实现应用的快速登录。仅在Android6.0中,谷歌提供了统一的指纹SDK接口。今天我们就来介绍一下指纹登录功能;1.App指纹登录介绍。指纹识别是Android6.0及以上版本支持的功能,而且类也不多,主要是FingerprintManager和它的三个内部类(AuthenticationCallback、AuthenticationResult、CryptoObject);指纹数据在手机的设置里,不存储在自己写的APP里;指纹识别只能识别,不能在APP中输入指纹,如果要输入指纹,可以在手机设置中的指纹功能中自行添加。比对指纹数据,然后执行成功和失败的回调,就这样;二、指纹登录步骤详解1、申请权限。Androidmanifest文件中指纹权限为2。验证手机是否支持指纹FingerprintManagerCompat提供了三个方法:isHardwareDetected()判断是否有硬件支持isKeyguardSecure()判断是否设置锁屏,因为一部手机至少要有两个登录方法hasEnrolledFingerprints()来判断系统是否至少添加一个指纹/***判断是否支持指纹识别*/publicstaticbooleansupportFingerprint(ContextmContext){if(Build.VERSION.SDK_INT<23){Toast.makeText(mContext,"你的系统版本太高了低支持指纹功能",Toast.LENGTH_SHORT).show();returnfalse;}else{KeyguardManagerkeyguardManager=mContext.getSystemService(KeyguardManager.class);FingerprintManagerCompatfingerprintManager=FingerprintManagerCompat.from(mContext);if(!fingerprintManager.isHardwareDetected()){Toast.makeText(mContext,"你的系统版本太低,不支持指纹功能",Toast.LENGTH_SHORT).show();returnfalse;}elseif(keyguardManager!=null&&!keyguardManager.isKeyguardSecure()){Toast.makeText(mContext,"您的手机不支持指纹功能",Toast.LENGTH_SHORT).show();returnfalse;}elseif(!fingerprintManager.hasEnrolledFingerprints()){Toast.makeText(mContext,"您需要在系统设置中添加至少一个指纹",Toast.LENGTH_SHORT).show();returnfalse;}}returntrue;}3.生成对称加密密钥valDEFAULT_KEY_NAME="default_key"lateinitvarkeyStore:KeyStoreprivatefuninitKey(){keyStore=KeyStore.getInstance("AndroidKeyStore")keyStore.load(null)valkeyGenerator=KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore")valbuilder=KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME,KeyProperties.PURPOSE_ENCRYPTorKeyProperties.PURPOSE_DECRYPT).setBlockModes(KeyProperties.BLOCK_MODE_CBC).setUserAuthenticationRequired(true).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)keyGenerator.init(builder.build())keyGenerator.generateKey()}4.生成Cipher对象privatefuninitCipher(mContext:Context?){valkey=keyStore.getKey(DEFAULT_KEY_NAME,null)asSecretKeyvalcipher=Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES+"/"+KeyProperties.BLOCK_MODE_CBC+"/"+KeyProperties.ENCRYPTION_PADDING_PKCS7)cipher.init(Cipher.ENCRYPT_MODE,key)}5、开启指纹验证privatevoidshowFingerPrintDialog(Contextcontext,Ciphercipher){FingerprintManagerCompatfingerprintManagerCompat=FingerprintManagerCompat.from(context);FingerprintManagerCompat.CryptoObjectcryptoObject=newFingerprintManagerCompat.CryptoObject(cipher);CancellationSignalmCancellationSignal=newCancellationSignal();//指纹识别过程中可以手动取消//mCancellationSignal.cancel();fingerprintManagerCompat.authenticate,Object(mCancellationSignal,newMyCallBack(),null);}6.指纹验证回调publicclassMyCallBackextendsFingerprintManagerCompat.AuthenticationCallback{@OverridepublicvoidonAuthenticationError(interrMsgId,CharSequenceerrString){//发生错误时回调此函数,例如多次尝试失败时,errString为错误信息//一般来说,我们先判断是否对手动取消Log.e("TAG","errMsgId="+errMsgId);if(errMsgId==FingerprintManager.FINGERPRINT_ERROR_LOCKOUT){Log.e("TAG",""+errString);}}//指纹验证时这个函数会失败时被回调。失败后允许多次尝试。如果故障太多,它会暂时停止响应,然后停止传感器的工作。@OverridepublicvoidonAuthenticationFailed(){//指纹认证失败,请重试Log.e("TAG","onAuthenticationFailed");}@OverridepublicvoidonAuthenticationHelp(inthelpMsgId,CharSequencehelpString){//出现错误时提示帮助,比如指纹错误,我们会显示在界面上,让用户了解情况Log.e("TAG","helpString="+helpString);}//当指纹验证成功时,会回调该函数,不再监听指纹传感器@OverridepublicvoidonAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResultresult){//这里我们可以做Log.e("TAG","onAuthenticationSucceeded="+result.toString());}}此时指纹验证登录流程为完了,是不是很简单;综上所述,关于指纹还有很多东西等待我们去了解,比如加密等;一起学习加油;