I want to integrate fingerprint lock into my application. The user has a choice to enable/disable the lock using switch. How can I achieve it programmatically.
For Example something like this:
So I have implemented this type of feature in my app but I don't know whether its a viable feature you may find an easy and sensible method but this is what I do
I use shared preferences for this so first in the activity where https://i.sstatic.net/3FY78.jpg exist I do like below
First I create this method
private void Biometric(){
androidx.biometric.BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
// this means we can use biometric sensor
case androidx.biometric.BiometricManager.BIOMETRIC_SUCCESS:
SharedPreferences sharedPreferences = getSharedPreferences("Authentication",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, "1");
editor.putBoolean(SWITCH1, Swicth_authenticate.isChecked());
editor.apply();
Intent intent = new Intent(AboutActivity.this, edit_profile.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
break;
// this means that the device doesn't have fingerprint sensor
case androidx.biometric.BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080101 Authentication failed there's no Fingerprint Reader in your device.", Toast.LENGTH_SHORT).show();
break;
// this means that biometric sensor is not available
case androidx.biometric.BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080102 Authentication failed biometric system not found.", Toast.LENGTH_SHORT).show();
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
Swicth_authenticate.setChecked(false);
Toast.makeText(this, "Error code 0x08080103 There's no password for this device.", Toast.LENGTH_SHORT).show();
break;
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
break;
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
break;
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
break;
}
}
And Activate the above method whenever a user turns on the checkbox And if user Deactivates I run the following in a method
SharedPreferences sharedPreferences = getSharedPreferences("Authentication",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, "0");
editor.putBoolean(SWITCH1, Swicth_authenticate.isChecked());
editor.apply();
As you can see I have used Shared Preferences in that method and in my launcher activity I do the following thing.
SharedPreferences sharedPreferences = getSharedPreferences("Authentication", 0);
String bio = sharedPreferences.getString(TEXT, "");
if (bio.equals("1")) {
BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | DEVICE_CREDENTIAL)) {
// this means we can use biometric sensor
case BiometricManager.BIOMETRIC_SUCCESS:
break;
// this means that the device doesn't have fingerprint sensor
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
break;
// this means that biometric sensor is not available
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
break;
// this means that the device doesn't contain your fingerprint
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
break;
case BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
break;
case BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED:
break;
case BiometricManager.BIOMETRIC_STATUS_UNKNOWN:
break;
}
// creating a variable for our Executor
Executor executor = ContextCompat.getMainExecutor(this);
// this will give us result of AUTHENTICATION
final BiometricPrompt biometricPrompt = new BiometricPrompt(StartActivity.this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
// THIS METHOD IS CALLED WHEN AUTHENTICATION IS SUCCESS
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(), "Login Success.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
// creating a variable for our promptInfo
// BIOMETRIC DIALOG
final BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder().setTitle("Authentication")
.setDescription("Use your fingerprint to login ")
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK | DEVICE_CREDENTIAL).build();
biometricPrompt.authenticate(promptInfo);
} else {
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
If you have any doubt regarding this comment below I know its very huge but I don't know how much you know so I pretty much wrote whole code even then if you have doubt you can see the following source code https://github.com/MohammedAbidNafi/MessengerByMargs