近期在做android指纹相关的功能,谷歌在android6.0及以上版本号对指纹识别进行了官方支持。当时在FingerprintManager和FingerprintManagerCompat这两个之间纠结。当中使用FingerprintManager要引入com.android.support:appcompat-v7包。考虑到包的大小,决定使用v4兼容包FingerprintManagerCompat来实现。

主要实现的工具类FingerprintUtil:验证手机是否支持指纹识别方法callFingerPrintVerify(),主要验证手机硬件是否支持(6.0及以上),有没有录入指纹,然后有没有开启锁屏password。開始验证对于识别成功,失败能够进行对应的回调处理。

 public class FingerprintUtil{

    private FingerprintManagerCompat mFingerprintManager;
private KeyguardManager mKeyManager;
private CancellationSignal mCancellationSignal;
private Activity mActivity; public FingerprintUtil(Context ctx) {
mActivity = (Activity) ctx;
mFingerprintManager = FingerprintManagerCompat.from(mActivity);
mKeyManager = (KeyguardManager) mActivity.getSystemService(Context.KEYGUARD_SERVICE); } public void callFingerPrintVerify(final IFingerprintResultListener listener) {
if (!isHardwareDetected()) {
return;
}
if (!isHasEnrolledFingerprints()) {
if (listener != null) {
listener.onNoEnroll();
}
return;
}
if (!isKeyguardSecure()) {
if (listener != null) {
listener.onInSecurity();
}
return;
}
if (listener != null) {
listener.onSupport();
} if (listener != null) {
listener.onAuthenticateStart();
}
if (mCancellationSignal == null) {
mCancellationSignal = new CancellationSignal();
}
try {
mFingerprintManager.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {
//多次尝试都失败会走onAuthenticationError。会停止响应一段时间。提示尝试次数过多。请稍后再试。 @Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
if (listener != null)
listener.onAuthenticateError(errMsgId, errString);
} //指纹验证失败走此方法,比如小米前4次验证失败走onAuthenticationFailed,第5次走onAuthenticationError
@Override
public void onAuthenticationFailed() {
if (listener != null)
listener.onAuthenticateFailed();
} @Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
if (listener != null)
listener.onAuthenticateHelp(helpMsgId, helpString); } //当验证的指纹成功时会回调此函数。然后不再监听指纹sensor
@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
if (listener != null)
listener.onAuthenticateSucceeded(result);
} }, null);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 是否录入指纹,有些设备上即使录入了指纹,可是没有开启锁屏password的话此方法还是返回false
*
* @return
*/
private boolean isHasEnrolledFingerprints() {
try {
return mFingerprintManager.hasEnrolledFingerprints();
} catch (Exception e) {
return false;
}
} /**
* 是否有指纹识别硬件支持
*
* @return
*/
public boolean isHardwareDetected() {
try {
return mFingerprintManager.isHardwareDetected();
} catch (Exception e) {
return false;
}
} /**
* 推断是否开启锁屏password
*
* @return
*/
private boolean isKeyguardSecure() {
try {
return mKeyManager.isKeyguardSecure();
} catch (Exception e) {
return false;
} } /**
* 指纹识别回调接口
*/
public interface IFingerprintResultListener {
void onInSecurity(); void onNoEnroll(); void onSupport(); void onAuthenticateStart(); void onAuthenticateError(int errMsgId, CharSequence errString); void onAuthenticateFailed(); void onAuthenticateHelp(int helpMsgId, CharSequence helpString); void onAuthenticateSucceeded(FingerprintManagerCompat.AuthenticationResult result); } public void cancelAuthenticate() {
if (mCancellationSignal != null) {
mCancellationSignal.cancel();
mCancellationSignal = null;
}
} public void onDestroy() {
cancelAuthenticate();
mKeyManager = null;
mFingerprintManager = null; }

參考了一些资料,做了一些验证。得到一些结论:

1、当指纹识别失败后,会调用onAuthenticationFailed()方法,这时候指纹传感器并没有关闭,谷歌原生系统给了我们5次重试机会,也就是说,连续调用了4次onAuthenticationFailed()方法后,第5次会调用onAuthenticateError(int errMsgId, CharSequence errString)方法,此时errMsgId==7。

2、每次又一次授权,哪怕不去校验。取消时会走onAuthenticateError(int errMsgId, CharSequence errString) 方法,当中errMsgId==5,
3、当系统调用了onAuthenticationError()和onAuthenticationSucceeded()后,传感器会关闭,仅仅有我们又一次授权。再次调用authenticate()方法后才干继续使用指纹识别功能。

4、兼容android6.0下面系统的话,不要使用FingerprintManagerCompat, 低于M的系统版本号。FingerprintManagerCompat不管手机是否有指纹识别模块,均觉得没有指纹识别,能够用FingerprintManager来做。
5、考虑到安全因素,最好authenticate(CryptoObject crypto, CancellationSignal cancel, int flags, AuthenticationCallback callback, Handler handler)时增加CryptoObject 。crypto这是一个加密类的对象,指纹扫描器会使用这个对象来推断认证结果的合法性。

这个对象能够是null,可是这种话。就意味着app无条件信任认证的结果,这个过程可能被攻击。数据能够被篡改。这是app在这种情况下必须承担的风险。

因此。建议这个參数不要置为null。这个类的实例化有点麻烦,主要使用javax的security接口实现。

Android6.0指纹识别开发的更多相关文章

  1. Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)

    指纹识别这个名词听起来并不陌生,但是实际开发过程中用得并不多.Google从Android6.0(api23)开始才提供标准指纹识别支持,并对外提供指纹识别相关的接口.本文除了能适配6.0及以上系统, ...

  2. Android开发学习之路-指纹识别api

    在android6.0之后谷歌对指纹识别进行了官方支持,今天还在放假,所以就随意尝试了一下这个api,但是遇到了各种各样的问题 ①在使用FingerPrintManager这个类实现的时候发现了很多问 ...

  3. 基于MFC开发的指纹识别系统.

    MFC-FingerPrint 基于MFC开发的指纹识别系统. 效果图如下: 在第12步特征入库中,会对当前指纹的mdl数据与databases中所有的mdl进行对比,然后返回识别结果. 一.载入图像 ...

  4. iOS开发——Touch ID 指纹识别

    项目中为了安全性,一般使用密码或iPhone手机的指纹识别Touch ID. 第一步,判断系统是否支持,iOS8.0及以上才支持. 第二步,判断手机是否支持,带Touch ID的手机iPhone5s及 ...

  5. ios开发-指纹识别

    最近我们使用支付宝怎么软件的时候,发现可以使用指纹了,看起来是否的高大上.当时苹果推出了相关接口,让程序写起来很简单哈. 在iPhone5s的时候,苹果推出了指纹解锁.但是在ios8.0的时候苹果才推 ...

  6. Android6.0之后的权限机制对App开发的影响

    随着Android系统的更新换代,每次重大更新的方面也逐步扩展,从4.*主要是增强功能,到5.*主要是美化界面,到6.*主要提高系统安全性,再到7.*和8.*主要支撑各种大屏设备,因此开发者需要对每个 ...

  7. iOS 。开发之指纹识别功能

    // 头文件导入 #import <LocalAuthentication/LocalAuthentication.h> //在iPhone5s的时候,苹果推出了指纹解锁.但是在ios8. ...

  8. 【Unity游戏开发】Android6.0以上的动态权限申请问题

    一.引子 最近公司的游戏在做安全性测试,期间也暴露出了不少安全上的问题.虽然我们今天要说的权限申请和安全性相关不大,但是也会影响到游戏的使用体验等,所以本篇博客中马三就想和大家谈谈Android6.0 ...

  9. Android开发学习之路-Android6.0运行时权限

    在Android6.0以后开始,对于部分敏感的“危险”权限,需要在应用运行时向用户申请,只有用户允许的情况下这个权限才会被授予给应用.这对于用户来说,无疑是一个提升安全性的做法.那么对于开发者,应该怎 ...

随机推荐

  1. Visual Studio for Mac 安装时无法连接到网络等问题

    问题: 1.下载 vs for mac 离线安装包 离线下载地址https://download.microsoft.com/download/3/d/4/3d42f40f-4f0a-4613-920 ...

  2. centos7yum安装tomcat

    执行安装命令 [root@localhost ~]# yum -y install tomcat 查询tomcat是否安装成功 [root@localhost ~]# rpm -q tomcat to ...

  3. DDD精彩

    MS STST 这难度太高了 有一个就很难的了 也许我工作的环境一般,能把SOLID简要描述一下的,都还没有遇到 SOLID还只属于OOD层次,OOA层面就更加没碰到了 Scrip 因为领域驱动设计的 ...

  4. 关于在eclipse下的mapreduce工程打包成jar包的问题(包含第三方jar包)

    这个问题也是在开发项目中经常遇到的一个问题,网上提供了很多方法,但是我发现很多并不适用,这里推荐两种方法,一种肯定没问题,就是比较麻烦,另一种是适用FatJar来打包,但是我没成功,原因估计出在ubu ...

  5. Flume学习应用:Java写日志数据到MongoDB

    概述 Windows平台:Java写日志到Flume,Flume最终把日志写到MongoDB. 系统环境 操作系统:win7 64 JDK:1.6.0_43 资源下载 Maven:3.3.3下载.安装 ...

  6. VMware Workstation的三种网络连接方式

    桥接模式(Bridged).NAT模式(地址转换模式).仅主机模式(Host-Only) 桥接模式就是将主机网卡与虚拟机的网卡利用虚拟网桥进行通信.在桥接的作用下,类似于把物理主机虚拟为一个交换机,所 ...

  7. Xamarin Forms启动自带模拟器缓慢

    Xamarin Forms启动自带模拟器缓慢 Xamarin Forms启动自带模拟器缓慢,在Windows 10中,Visual Studio可以使用系统自带的Hyper模拟器.但是使用时候,会长时 ...

  8. smail修改字符串 汉字

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com 将smali文件中的unicode字符串变为汉字

  9. 【UOJ #34】多项式乘法

    http://uoj.ac/problem/34 看了好长时间的FFT和NTT啊qwq在原根那块磨蹭了好久_(:з」∠)_ 首先设答案多项式的长度拓展到2的幂次后为n,我们只要求出一个g(不是原根)满 ...

  10. bzoj 2038 小Z的袜子(hose)(莫队算法)

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 11542  Solved: 5166[Sub ...