亲宝软件园·资讯

展开

Android指纹登录工具类 Android指纹登录工具类封装

LeeStudio_ 人气:0
想了解Android指纹登录工具类封装的相关内容吗,LeeStudio_在本文为您仔细讲解Android指纹登录工具类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android指纹登录工具类,Android指纹登录,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();

  }
}

加载全部内容

相关教程
猜你喜欢
用户评论