本文实例为大家分享了Android指纹登录工具类的封装代码,供大家参考,具体内容如下
核心
Android 指纹 是在 6.0 才出来的一个重要的功能
复制代码
1@RequiresApi(api = Build.VERSION_CODES.M)
核心的两个 api:
FingerprintManager
KeyguardManager
step1
判断android 版本,如果小于 6.0 支持不了指纹
复制代码
1
2
3if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ return; }
step2
判断 手机硬件(有没有指纹感应区)就是手机是否支持传感
复制代码
1
2
3
4
5
6
7
8@RequiresApi(api = Build.VERSION_CODES.M) public boolean isHardFinger() { if (fingerprintManager != null && fingerprintManager.isHardwareDetected()) { return true; } else { return false; } }
step3
检查手机是否开启锁屏密码(如手机未开锁,涉及到一个优先级问题,先解锁 后使用)
复制代码
1
2
3
4
5
6
7public boolean isWindowSafe() { if (keyguardManager != null && keyguardManager.isKeyguardSecure()) { return true; } else { return false; } }
step4
检查手机是否有录入指纹
复制代码
1
2
3
4
5
6
7
8@RequiresApi(api = Build.VERSION_CODES.M) public boolean isHaveHandler() { if (fingerprintManager != null && fingerprintManager.hasEnrolledFingerprints()) { return true; } else { return false; } }
只有以上步骤全满足,才能使用指纹
开启指纹验证
复制代码
1
2
3
4
5
6
7
8@RequiresApi(api = Build.VERSION_CODES.M) public void authenticate(FingerprintManager.CryptoObject cryptoObject, CancellationSignal cancellationSignal, int flag, FingerprintManager.AuthenticationCallback authenticationCallback, Handler handler) { if (fingerprintManager != null) { fingerprintManager.authenticate(cryptoObject, cancellationSignal, flag, authenticationCallback, handler); } }
参数中最重要的就是 cancellationSignal和 callback,其他传null 和 0 就行,cancellationsignal 是用来取消指纹验证的,而callback 可以回调 指纹验证失败次数 或者指纹验证成功
最后附上简单工具类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86/** * 指纹识别工具类 */ public class FingerUtils { private final FingerprintManager fingerprintManager; private final KeyguardManager keyguardManager; @RequiresApi(api = Build.VERSION_CODES.M) private FingerUtils(Context context) { fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE); keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); } private static FingerUtils singleton = null; @RequiresApi(api = Build.VERSION_CODES.M) public static FingerUtils getInstance(Context context) { if (singleton == null) { synchronized (FingerUtils.class) { if (singleton == null) { singleton = new FingerUtils(context); } } } return singleton; } /** * ②检查手机硬件(有没有指纹感应区) */ @RequiresApi(api = Build.VERSION_CODES.M) public boolean isHardFinger() { if (fingerprintManager != null && fingerprintManager.isHardwareDetected()) { return true; } else { return false; } } /** * ③检查手机是否开启锁屏密码 */ public boolean isWindowSafe() { if (keyguardManager != null && keyguardManager.isKeyguardSecure()) { return true; } else { return false; } } /** * ④检查手机是否已录入指纹 */ @RequiresApi(api = Build.VERSION_CODES.M) public boolean isHaveHandler() { if (fingerprintManager != null && fingerprintManager.hasEnrolledFingerprints()) { return true; } else { return false; } } /** * 创建指纹验证 */ @RequiresApi(api = Build.VERSION_CODES.M) public void authenticate(FingerprintManager.CryptoObject cryptoObject, CancellationSignal cancellationSignal, int flag, FingerprintManager.AuthenticationCallback authenticationCallback, Handler handler) { if (fingerprintManager != null) { fingerprintManager.authenticate(cryptoObject, cancellationSignal, flag, authenticationCallback, handler); } } /** * 取消指纹验证 . 应该不会用上 */ public void cannelFinger(CancellationSignal cancellationSignal) { cancellationSignal.cancel(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是光亮百褶裙最近收集整理的关于Android指纹登录工具类封装的全部内容,更多相关Android指纹登录工具类封装内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复