Android6.0指纹识别开发
近期在做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指纹识别开发的更多相关文章
- Android指纹识别深入浅出分析到实战(6.0以下系统适配方案)
指纹识别这个名词听起来并不陌生,但是实际开发过程中用得并不多.Google从Android6.0(api23)开始才提供标准指纹识别支持,并对外提供指纹识别相关的接口.本文除了能适配6.0及以上系统, ...
- Android开发学习之路-指纹识别api
在android6.0之后谷歌对指纹识别进行了官方支持,今天还在放假,所以就随意尝试了一下这个api,但是遇到了各种各样的问题 ①在使用FingerPrintManager这个类实现的时候发现了很多问 ...
- 基于MFC开发的指纹识别系统.
MFC-FingerPrint 基于MFC开发的指纹识别系统. 效果图如下: 在第12步特征入库中,会对当前指纹的mdl数据与databases中所有的mdl进行对比,然后返回识别结果. 一.载入图像 ...
- iOS开发——Touch ID 指纹识别
项目中为了安全性,一般使用密码或iPhone手机的指纹识别Touch ID. 第一步,判断系统是否支持,iOS8.0及以上才支持. 第二步,判断手机是否支持,带Touch ID的手机iPhone5s及 ...
- ios开发-指纹识别
最近我们使用支付宝怎么软件的时候,发现可以使用指纹了,看起来是否的高大上.当时苹果推出了相关接口,让程序写起来很简单哈. 在iPhone5s的时候,苹果推出了指纹解锁.但是在ios8.0的时候苹果才推 ...
- Android6.0之后的权限机制对App开发的影响
随着Android系统的更新换代,每次重大更新的方面也逐步扩展,从4.*主要是增强功能,到5.*主要是美化界面,到6.*主要提高系统安全性,再到7.*和8.*主要支撑各种大屏设备,因此开发者需要对每个 ...
- iOS 。开发之指纹识别功能
// 头文件导入 #import <LocalAuthentication/LocalAuthentication.h> //在iPhone5s的时候,苹果推出了指纹解锁.但是在ios8. ...
- 【Unity游戏开发】Android6.0以上的动态权限申请问题
一.引子 最近公司的游戏在做安全性测试,期间也暴露出了不少安全上的问题.虽然我们今天要说的权限申请和安全性相关不大,但是也会影响到游戏的使用体验等,所以本篇博客中马三就想和大家谈谈Android6.0 ...
- Android开发学习之路-Android6.0运行时权限
在Android6.0以后开始,对于部分敏感的“危险”权限,需要在应用运行时向用户申请,只有用户允许的情况下这个权限才会被授予给应用.这对于用户来说,无疑是一个提升安全性的做法.那么对于开发者,应该怎 ...
随机推荐
- CentOS7.5更改grub2菜单背景&开机动态画面
Grub2菜单背景 红帽企业版 Linux 7 的引导装载程序是“GRUB 2”.您可以更改“GRUB 2”外观的几个部分.以下几小节将向您展示如何改变 Linux 发行版名称.菜单颜色,和背景图片. ...
- CentOS 7 上搭建 ownCloud 私有云
所需软件 & 环境 操作系统:CentOS 7.3.1711 最小安装 (已关闭 SELinux 和防火墙) 应用软件: Nginx .MariaDB .PHP .ownCloud 10 ...
- 在Pygtk和Glade使用Gtkbuilder
最近开始学习python的GUI,选择了Pygtk,试着用Glade设计界面,项目文件采用Gtkbuilder格式,网上的教程大部分是使用Libglade,所以用xml方式读取.glade文件: wT ...
- thinkphp之自动完成
1.自动完成 自动完成是ThinkPHP提供用来完成数据自动处理和过滤的方法,使用create方法创建数据对象的时候会自动完成数据处理. 因此,在ThinkPHP使用create方法来创建数据对象是 ...
- HDFS上传文件错误--hdfs:DFSClient:DataStreamer Exception
今天上传文件的时候发现传上去的文件为空,错误提示如上述所示,原来是IP地址改掉了对呀应etc/hosts下面的IP地址也要改变,永久改ip命令-ifconfig eth0 xxx·xxx·xxx·xx ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)
Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- c++风格
http://web.archive.org/web/20160430022340/http://google.github.io/styleguide/cppguide.html 主要注意几点: 函 ...
- 初见Python<2>:列表和元组
1.在python中,最基本的数据结构是序列,序列中每一个元素被分配一个序号,即元素的位置,称为索引.索引从0开始,-1表示倒数第一个元素,-2表示倒数第二个元素,因此既可以是从前到后开始对元素进 ...
- [ARC097F]Monochrome Cat
题意:一棵树,每个节点是黑色或白色,你可以从任意节点开始进行一些操作并在任意节点结束,如果当前在$x$,那么一次操作可以是:1.走到相邻节点$y$并翻转$y$的颜色,2.翻转$x$的颜色,问把所有节点 ...
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 1) Introduction
最近学习了coursera上面Andrew NG的Machine learning课程,课程地址为:https://www.coursera.org/course/ml 在Introduction部分 ...