Tooth uncle tutorial is easy to understand

Results show

The environment

Autojs version: 9.0.5

Android version: 10.0.0

MIUI version: 12.5.1

Autojs9 does not have fingerprint authentication permissions, which need to be added in androidmanifest.xml using mt manager

android.permission.USE_BIOMETRIC

Here’s what you’ll learn

  • Fingerprint verification
  • ImgView Change the color

The code on

1. Import the classes
importClass("android.hardware.fingerprint.FingerprintManager");
importClass("android.os.CancellationSignal");
importClass("android.graphics.PorterDuff");
Copy the code
2. The UI interface
ui.layout(
  <vertical>
    <text text="Fingerprint verification" margin="0 0 0 10" textColor="#d9000000" textSize="35sp" w="*" gravity="center"></text>
    <text
      text="-- Uncle Teeth tutorial easy to understand"
      margin="0 0 0 10"
      textColor="# 80000000"
      textSize="25sp"
      w="*"
      gravity="center"
    ></text>
    <frame w="*" h="*">
      <img id="img" layout_gravity="center" src="@drawable/ic_fingerprint_black_48dp" w="100dp" h="100dp"></img>
    </frame>
  </vertical>
);
Copy the code
3. The main logic
// Check whether the device has fingerprint authentication
let r = judgeFingerprintIsCorrect();
if (r) {
  log("Start fingerprint verification.");
  let authenticationCallback = createAuthenticationCallback();
  mManager.authenticate(null, mCancellationSignal, 0, authenticationCallback, null);
}
Copy the code
4. Release resources
ui.emitter.on("pause".function () {
  release();
});
function release() {
  if(mCancellationSignal ! =null) {
    mCancellationSignal.cancel();
    mCancellationSignal = null; }}Copy the code
5. Validate the callback
function createAuthenticationCallback() {
  let mSelfCancelled = new FingerprintManager.AuthenticationCallback({
    onAuthenticationError: function (errorCode, errString) {
      // Enter this method after multiple fingerprint password authentication errors; And, not reinspected (for a short time)
      ErrorCode is the number of failures
      toastLog("Too many attempts, please try again later");
    },

    onAuthenticationHelp: function (helpCode, helpString) {
      // Fingerprint verification failed, but can be checked again, fingers may be dirty, or move too fast, etc.
      toastLog("Fingerprint verification failed");
      failureEffect(imgView);
    },

    onAuthenticationSucceeded: function (result) {
      toastLog("Fingerprint verification successful.");
      release();
      engines.myEngine().forceStop();
    },

    onAuthenticationFailed: function () {
      // Fingerprint verification fails. Fingerprint identification fails. You can recheck the fingerprint because the fingerprint is not recorded in the system.
      toastLog("Fingerprint verification failed"); failureEffect(imgView); }});return mSelfCancelled;
}
Copy the code
6. Check whether fingerprint verification is performed
function judgeFingerprintIsCorrect() {
  if(! mFingerprintManager.isHardwareDetected()) { toastLog("No fingerprint identification module.");
    return false;
  } else {
    log("With a fingerprint identification module.");
  }

  if(! mKeyManager.isKeyguardSecure()) { toastLog("Screen lock password not enabled");
    return false;
  } else {
    log("Has a screen-lock password.");
  }

  if(! mManager.hasEnrolledFingerprints()) { toastLog("There were no prints.");
    return false;
  } else {
    log("There were fingerprints.");
  }
  return true;
}
Copy the code

Quotes.

Thinking is the most important, other Baidu, Bing, StackOverflow, Android documents, autoJS documents, and finally in the group to ask — fang Shu tutorial

The statement

This tutorial is intended for learning purposes only and is not intended for any other use