Android 指纹登录工具类封装

核心

Android 指纹 是在 6.0 才出来的一个重要的功能

@RequiresApi(api = Build.VERSION_CODES.M)

核心的两个 api:

FingerprintManager
KeyguardManager

step1

判断android 版本,如果小于 6.0 支持不了指纹

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
return;
}

step2

判断 手机硬件(有没有指纹感应区)就是手机是否支持传感

  @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean isHardFinger() {
        if (fingerprintManager != null && fingerprintManager.isHardwareDetected()) {
            return true;
        } else {
            return false;
        }
    }

step3

检查手机是否开启锁屏密码(如手机未开锁,涉及到一个优先级问题,先解锁 后使用)

  public boolean isWindowSafe() {
        if (keyguardManager != null && keyguardManager.isKeyguardSecure()) {
            return true;
        } else {
            return false;
        }
    }

step4

检查手机是否有录入指纹

  @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);
        }
    }

参数中最重要的就是 cancellationSignal和 callback,其他传null 和 0 就行,
cancellationsignal  是用来取消指纹验证的,而callback 可以回调 指纹验证失败次数 或者指纹验证成功、

最后附上简单工具类

/**
 * 指纹识别工具类
 */
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();

    }
}

`

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,516评论 2 59
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,420评论 25 709
  • 1.下列哪些语句关于内存回收的说明是正确的? (b )A、 程序员必须创建一个线程来释放内存B、内存回收程序负责释...
    悠悠我心_4a00阅读 3,619评论 0 2
  • 1.什么是Activity?问的不太多,说点有深度的 四大组件之一,一般的,一个用户交互界面对应一个activit...
    JoonyLee阅读 11,041评论 2 51
  • 有时候越是美好的回忆,越不敢去回忆, 因为深知已是过去,再去怀念,越是想念,越是遗憾;直到有一天相似的情景出现,仿...
    空凭祭阅读 1,783评论 0 2

友情链接更多精彩内容