SoFunction
Updated on 2025-04-10

Analysis of Android fingerprint unlocking method

Let me first talk about the differences between the two methods
The first type:
After the transfer is successful, you will not be allowed to unlock your fingerprint, but will be transferred to the gesture unlock page when you set up fingerprint unlock.
The second type:
After the transfer is successful, fingerprint unlocking is performed. If you don’t turn, you put your fingers on the metal sensing ring to verify fingerprints.
You can choose according to your needs

OK Then the code will be displayed

The first type:
xml layout: a text display A button (not explained)

Source code

public class MainActivity extends FragmentActivity { 
FingerprintManager manager; 
KeyguardManager mKeyManager; 
private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0; 
private final static String TAG = “finger_log”;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setContentView(.activity_main); 
  //1: Get the fingerprint recognition manager first, see if the acquisition method is the same as the mathematical formula.  (xxxManager)=(Serve)
  manager = (FingerprintManager) (Context.FINGERPRINT_SERVICE); 
  mKeyManager = (KeyguardManager) (Context.KEYGUARD_SERVICE); 
// 2 Initialize button Set listening// Monitoring Monitoring What to listen to? I must listen to whether this phone has fingerprint recognition function. So check isFinger()
  Button btn_finger = (Button) findViewById(.btn_activity_main_finger); 
  btn_finger.setOnClickListener(new () { 
    @Override 
    public void onClick(View v) { 
      if (isFinger()) { 
        (, "Please do fingerprint recognition", Toast.LENGTH_LONG).show(); 
        Log(TAG, "keyi"); 
        startListening(null); 
      } 
    } 
  }); 

} 

public boolean isFinger() { 

  // On Android studio, if there is no such thing, an error will be reported  if ((this, .USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { 
    (this, "No fingerprint recognition permission", Toast.LENGTH_SHORT).show(); 
    return false; 
  } 
  Log(TAG, "Fingerprint permission"); 
  //Discern whether the hardware supports fingerprint recognition  if (!()) { 
    (this, "No fingerprint recognition module", Toast.LENGTH_SHORT).show(); 
    return false; 
  } 
 Log(TAG, "Fingerprint module"); 
  //Judge whether to enable the lock screen password
  if (!()) { 
    (this, "No lock screen password is enabled", Toast.LENGTH_SHORT).show(); 
    return false; 
  } 
  Log(TAG, "Lock screen password enabled"); 
  //Judge whether there is fingerprint input  if (!()) { 
    (this, "No fingerprint entered", Toast.LENGTH_SHORT).show(); 
    return false; 
  } 
  Log(TAG, "Fingerprint entered"); 

  return true; 
} 

CancellationSignal mCancellationSignal = new CancellationSignal(); 
//Callback method mSelfCancelled = new () { 
  @Override 
  public void onAuthenticationError(int errorCode, CharSequence errString) { 
    //But after multiple fingerprint password verification errors, enter this method; and, fingerprint verification cannot be called in a short time    (, errString, Toast.LENGTH_SHORT).show(); 
    showAuthenticationScreen(); 
  } 

  @Override 
  public void onAuthenticationHelp(int helpCode, CharSequence helpString) { 

    (, helpString, Toast.LENGTH_SHORT).show(); 
  } 

  @Override 
  public void onAuthenticationSucceeded( result) { 

    (, "Fingerprint recognition successfully", Toast.LENGTH_SHORT).show(); 
  } 

  @Override 
  public void onAuthenticationFailed() { 
    (, "Fingerprint recognition failed", Toast.LENGTH_SHORT).show(); 
  } 
}; 


public void startListening( cryptoObject) { 
  // On Android studio, if there is no such thing, an error will be reported  if ((this, .USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { 
    (this, "No fingerprint recognition permission", Toast.LENGTH_SHORT).show(); 
    return; 
  } 
  (cryptoObject, mCancellationSignal, 0, mSelfCancelled, null); 


} 

/**
  * Lock screen password
  */ 
private void showAuthenticationScreen() { 

  Intent intent = ("finger", "Test fingerprint recognition"); 
  if (intent != null) { 
    startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); 
  } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
  if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { 
    // Challenge completed, proceed with using cipher 
    if (resultCode == RESULT_OK) { 
      (this, "Identification successfully", Toast.LENGTH_SHORT).show(); 
    } else { 
      (this, "Recognition failed", Toast.LENGTH_SHORT).show(); 
    } 
  } 
} 

private void Log(String tag, String msg) { 
  (tag, msg); 
} 
}

===================================

The second type:
1: xml: Layout One text prompt Two buttons (1: Start recognition 2: Unrecognition)
2: We put the business operations into a FingerprintController class

private static FingerprintController sSingleton = null;

private Context mContext;

private FingerprintManagerCompat manager;

private static final String TAG = "FingerprintController";
private static final String PREMISSION = ".USE_FINGERPRINT";

//successpublic static final int FINGER_SUCCESS = 0;
//The hardware does not support itpublic static final int FINGER_ERROR_NO_HARDWARE = 1;
//No application permissionpublic static final int FINGER_ERROR_NO_PERMISSION = 2;
//The user does not grant permission//Protection level: normal
//The level of fingerprint permission is normal, theoretically, dynamic permission authentication is not requiredpublic static final int FINGER_ERROR_NO_USER_PERMISSION = 3;
//The user does not store fingerprintspublic static final int FINGER_ERROR_NO_FINGER = 4;

//Cancel fingerprint recognitionprivate CancellationSignal cancellationSignal;

private FingerAuthListener mAuthListener;

public static synchronized FingerprintController getInstance(Context context) {
  if (sSingleton == null) {
    sSingleton = new FingerprintController(context);
  }
  return sSingleton;
}

public FingerprintController(Context context) {
  mContext = context;
  manager = ();
}

/**
  * Start fingerprint recognition
  * It takes a certain amount of time to restart after too many failures
  */
public void startFingerAuth() {
  if (null == cancellationSignal) {
    cancellationSignal = new CancellationSignal();
  }
  (null, 0, cancellationSignal, new FingerAuthCallBack(), null);
}

/**
  * Cancel fingerprint recognition
  */
public void cancelFingerAuth() {
  if (cancellationSignal != null) {
    ();
    if (mAuthListener != null)
      ();
  }
}

/**
  * Fingerprint recognition callback
  */
public class FingerAuthCallBack extends  {

  // Call back this function when an error occurs, such as when multiple attempts fail, errString is an error message  @Override
  public void onAuthenticationError(int errMsgId, CharSequence errString) {
// (TAG, “onAuthenticationError: ” + errString); 
if (null != mAuthListener) 
(()); 
}

  // This function will be called back when fingerprint verification fails. After failure, multiple attempts are allowed. Too many failures will stop responding for a period of time and then stop the sensor's work  @Override
  public void onAuthenticationFailed() {
    if (null != mAuthListener)
      ();
  }

  @Override
  public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
    if (null != mAuthListener)

      ("helpMsgId",helpMsgId+"");
      ("helpString",());
      (());
  }

  // This function will be called back when the verified fingerprint is successful, and then the fingerprint sensor will no longer be monitored  @Override
  public void onAuthenticationSucceeded( result) {

   if (null != mAuthListener)
      ();

  }

}

/**
  * Check if fingerprint unlocking is available
  *
  * @return status
  */
public int checkFingerEnable() {
  if (null == manager) {
    manager = (mContext);
  }
  if (!isAppPermissionEnable()) {
    return FINGER_ERROR_NO_PERMISSION;
  }
  if (!()) {
    return FINGER_ERROR_NO_HARDWARE;
  }
  if (!()) {
    return FINGER_ERROR_NO_FINGER;
  }
  if (!isUserPermissionEnable()) {
    return FINGER_ERROR_NO_USER_PERMISSION;
  }
  return FINGER_SUCCESS;
}

/**
  * Whether the permission is declared
  */
private boolean isAppPermissionEnable() {
  PackageManager pm = ();
  if (pm == null) {
    (TAG, "can't get packagemanager");
    return true;
  }
  try {
    return PackageManager.PERMISSION_GRANTED == (PREMISSION, ());
  } catch (Exception e) {
    (TAG, "can't checkt Permission " + ());
    return true;
  }
}

/**
  * Whether it has dynamic permissions, theoretically no verification is required
  */
private boolean isUserPermissionEnable() {
  if (.SDK_INT >= Build.VERSION_CODES.M) {
    return PackageManager.PERMISSION_GRANTED == (.USE_FINGERPRINT);
  }
  return true;
}

public void setAuthListener(FingerAuthListener authListener) {
  mAuthListener = authListener;
}

public interface FingerAuthListener {

  void success();

  void error(String error);

  void help(String msg);

  void cancel();

  void failure();

}
} 

3: Implement this callback interface in main

private int code = FingerprintController.FINGER_SUCCESS;

private TextView toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);

 toast = (TextView) findViewById();

 code = (this).checkFingerEnable();
 if (code == FingerprintController.FINGER_SUCCESS) {
  (this).setAuthListener(this);
  setToast("Can enable fingerprint recognition");
 } else {
  switch (code) {
   case FingerprintController.FINGER_ERROR_NO_HARDWARE:
    setToast("This device does not support fingerprint recognition");
    break;
   case FingerprintController.FINGER_ERROR_NO_PERMISSION:
    setToast("The current application does not have fingerprint recognition permission");
    break;
   case FingerprintController.FINGER_ERROR_NO_FINGER:
    setToast("The current device has not entered the fingerprint, please go to enter the fingerprint");
    break;
  }
 }

}

public void start(View view) {
 if (code == FingerprintController.FINGER_SUCCESS) {
  (this).startFingerAuth();
  setToast("Start fingerprint recognition");
 }
}

public void cancel(View view) {
 if (code == FingerprintController.FINGER_SUCCESS) {
  (this).cancelFingerAuth();
 }
}


@Override
public void success() {
 setToast("Identification successfully");
}

@Override
public void error(String error) {
 setToast(error);
}

@Override
public void help(String msg) {
 setToast(msg);
}

@Override
public void cancel() {
 setToast("Cancel fingerprint recognition");
}

@Override
public void failure() {
 setToast("Fingerprint recognition failed");
}

public void setToast(String msg) {
 ("hint:" + msg);
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.