参考别人的总结,简单的实现了指纹识别的功能,下面做个小小的总结
简单效果图
指纹识别的必要条件:仅支持API23及以上的系统,设备必须带有指纹识别功能
简单的业务逻辑:
1 判断设备的API是否大于22
得到系统的API
minSdkVersion= Build.VERSION.SDK;
2 添加指纹识别权限
3 判断设备是否支持指纹识别
在开始判断之前需要先通过V4包获得对象
manager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);
keyManager= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
判断
//硬件设备是否支持指纹解锁功能
if (!manager.isHardwareDetected()) {
Toast.makeText(getBaseContext(), "该手机不支持指纹解锁", Toast.LENGTH_SHORT).show();
return false;
}
4 判断设备是否设置了锁屏密码
//判断是否有锁屏密码
if(!keyManager.isKeyguardSecure()){
Toast.makeText(getBaseContext(), "请设置锁屏密码", Toast.LENGTH_SHORT).show();
return false;
}
5 判断设备是否录入指纹,貌似APP无法直接唤醒指纹设置页面
//判断是否录入指纹
if (!manager.hasEnrolledFingerprints()) {
Toast.makeText(getBaseContext(), "没有录入指纹", Toast.LENGTH_SHORT).show();
return false;
}
参数分别是:防止第三方恶意攻击的包装类,CancellationSignal对象,flags,回调对象,handle
manager.authenticate(cryptoObject, signal, 0, callBack, null);
7 在回调函数里做识别后逻辑处理
回调函数的常用方法
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(getBaseContext(), "操作过于频繁,请稍后再试",Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
}
//指纹识别成功
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getBaseContext(), "指纹识别成功",Toast.LENGTH_SHORT).show();
image.setImageResource(R.mipmap.yes);
}
//指纹识别失败
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getBaseContext(), "指纹识别失败", Toast.LENGTH_SHORT).show();
i++;
if(i==3){
Toast.makeText(getBaseContext(), "失败次数过多,请输入锁屏密码", Toast.LENGTH_SHORT).show();
showLockScreenPass();
i=0;
}
}
};
8 如果识别失败次数过多,则转入输入解锁密码界面
/**
*指纹识别错误次数过多,显示手机锁屏密码
*/
private void showLockScreenPass() {
Intent intent=keyManager.createConfirmDeviceCredentialIntent("finger","开启锁屏密码");
if(intent!=null){
startActivityForResult(intent, REQUST_CODE);
}
}
源码下载地址:
http://download.csdn.net/detail/huiling815/9624163