近期在做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. python 网络爬虫框架scrapy使用说明

    1 创建项目scrapy startproject tutorial 2 定义Itemimport scrapyclass DmozItem(scrapy.Item):    title = scra ...

  2. css自媒体查询

    准备工作1:设置Meta标签 首先我们在使用Media的时候需要先设置下面这段代码,来兼容移动设备的展示效果: <meta name="viewport" content=& ...

  3. VS2015运行 cordova 时候提示无法运行解决

    出现此问题的原因是在于npm程序损坏了.vs调用的npm程序并不是在node安装目录下的npm,而是在: C:\Users\用户名\AppData\Roaming\Microsoft\VisualSt ...

  4. win_tc使用感受

    上大学的时候一直在使用win_tc就因为使用方便,今天准备用这个工具编辑一个函数,就特意下载了一个. 没想到直接出来一个bug. sizeof(char*)结果竟然是2. 果断接卸,误人子弟啊.

  5. XML--读写操作

    XML文档的相关操作 1.配置文件:在实际项目开发中,XML作为配置文件是不可取代的(框架中的部分功能可以以注解形式来取代) (1) 不同技术,XML配置文件的作用也不一样. (2) 比如当前和这个阶 ...

  6. bzoj 2803 [Poi2012]Prefixuffix 兼字符串hash入门

    打cf的时候遇到的问题,clairs告诉我这是POI2012 的原题..原谅我菜没写过..于是拐过来写这道题并且学了下string hash.   字符串hash基于Rabin-Karp算法,并且对于 ...

  7. (原创)Stanford Machine Learning (by Andrew NG) --- (week 6) Advice for Applying Machine Learning & Machine Learning System Design

    (1) Advice for applying machine learning Deciding what to try next 现在我们已学习了线性回归.逻辑回归.神经网络等机器学习算法,接下来 ...

  8. 探究Activity(1)--Activity的基本用法

    一.Activity是什么 Activity(活动)是最容易吸引用户的地方,它是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用程序中应该包括零个或多个Activity. 二.Activi ...

  9. [bzoj1010](HNOI2008)玩具装箱toy(动态规划+斜率优化+单调队列)

    Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有 的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1.. ...

  10. [转]Spring配置之OpenSessionInViewFilter

    参考: OpenSessionInViewFilter作用及配置:http://www.yybean.com/opensessioninviewfilter-role-and-configuratio ...