Android Fingerprint Login Tool Class Encapsulation

  • 2021-12-09 09:50:49
  • OfStack

This article example for everyone to share the Android fingerprint login tool class encapsulation code, for your reference, the specific content is as follows

Core

Android fingerprint is an important feature that came out in 6.0


@RequiresApi(api = Build.VERSION_CODES.M)

The two api of the core:

FingerprintManager
KeyguardManager

step1

Judge android version, if it is less than 6.0, it can't support fingerprint


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

step2

Judge the hardware of the mobile phone (whether there is a fingerprint sensing area), that is, whether the mobile phone supports sensing


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

step3

Check whether the mobile phone opens the screen lock password (if the mobile phone is not unlocked, it involves a priority problem, unlock it first and then use it)


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

step4

Check whether the mobile phone has entered fingerprints


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

Fingerprints can only be used if all the above steps are met

Turn on fingerprint verification


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

Parameters of the most important is cancellationSignal and callback, other null and 0 line, cancellationsignal is used to cancel fingerprint verification, and callback can call back the number of fingerprint verification failures or fingerprint verification success

Finally, a simple tool class is attached


/**
 *  Fingerprint identification tool class 
 */
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;
  }


  /**
   *  ② Check the mobile phone hardware (whether there is a fingerprint sensing area) 
   */

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

  /**
   *  ③ Check whether the mobile phone opens the screen lock password 
   */

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

  /**
   *  ④ Check whether the mobile phone has entered fingerprints 
   */
  @RequiresApi(api = Build.VERSION_CODES.M)
  public boolean isHaveHandler() {
    if (fingerprintManager != null && fingerprintManager.hasEnrolledFingerprints()) {
      return true;
    } else {
      return false;
    }
  }

  /**
   *  Create fingerprint authentication 
   */
  @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);
    }
  }

  /**
   *  Cancel fingerprint verification  .  It should not be used 
   */
  public void cannelFinger(CancellationSignal cancellationSignal) {
    cancellationSignal.cancel();

  }
}

Related articles: