Implementation code of adding fingerprint unlocking function in Android
- 2021-09-16 08:05:04
- OfStack
Preface
Fingerprint unlocking technology has become an important means to verify user information at present, and basically all mobile phones are equipped with fingerprint unlocking. When APP needs encryption verification, we can consider adding system fingerprint unlocking function.
The steps of adding fingerprint unlocking function are very simple, and the general process is as follows:
1 Add Permissions
Add permission to access the user's fingerprint in the Manifest. xml file.
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
2 Declare fingerprint management class objects provided by the system
private FingerprintManagerCompat manager;
3 Get fingerprint management class objects
manager = FingerprintManagerCompat.from(this);
4 Perform the verification process
manager.authenticate(null, 0, null, new FingerAuthenticateCallBack(), null);
5 Monitor fingerprint verification results
Fingerprint verification results are passed to developers by callback, which requires developers to inherit AuthenticationCallback class. The specific methods are as follows:
public class FingerAuthenticateCallBack extends FingerprintManagerCompat.AuthenticationCallback {
private static final String TAG = "FingerAuthenticateCallBack";
// Callback this function when an error occurs, such as when many attempts fail, errString Is an error message
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
Log.e(TAG, "onAuthenticationError: " + errString);
}
// When fingerprint verification fails, this function will be called back. After failure, multiple attempts are allowed. If the number of failures is too high, the response will stop 1 For a while and then stop sensor Work of
@Override
public void onAuthenticationFailed() {
Log.d(TAG, "onAuthenticationFailed: " + " Validation failed ");
}
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Log.e(TAG, "onAuthenticationHelp: " + helpString);
}
// This function is called back when the verified fingerprint is successful, and then the fingerprint is no longer monitored sensor
@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult
result) {
Log.e(TAG, "onAuthenticationSucceeded: " + " Verification succeeded ");
}
}
Summarize