Можно ли сканировать отпечаток пальца в автоматическом режиме, не открывая диалоговое окно, предоставляемое системой, и не рекомендуется FingerPrintManager
на api 28+?
BiometricPrompt без диалогового окна, предоставляемого системой
Ответы (1)
Эй,
Я сделал то, о чем вы говорите, только для меня, и теперь время поделиться с вами, и это просто.
Просто смотрите !!
private final String KEY_STORE = "AndroidKey";
private void CheckRequirementAvailability() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (!fingerprintManager.isHardwareDetected()) {
login_fingerprint_status.setText("Fingerprint scanner not detected in your device.");
} else if (ContextCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
login_fingerprint_status.setText("Allow app permission to use Fingerprint Authentication");
} else if (!keyguardManager.isKeyguardSecure()) {
login_fingerprint_status.setText("Your device was not secured by any security");
} else if (!fingerprintManager.hasEnrolledFingerprints()) {
login_fingerprint_status.setText("Enroll at least one fingerprint to your device");
} else {
login_fingerprint_status.setText("Place your finger on the scanner to proceed");
generateKey();
if (cipherInit()) {
FingerprintManager.CryptoObject cryptoObject = (FingerprintManager.CryptoObject) new FingerprintManager.CryptoObject(cipher);
CancellationSignal cancellationSignal = new CancellationSignal();
fingerprintManager.authenticate(cryptoObject, cancellationSignal, 0, authenticationCallback(), null);
}
}
}
}
@TargetApi(Build.VERSION_CODES.M)
public void generateKey() {
try {
keyStore = KeyStore.getInstance("AndroidKeyStore");
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyStore.load(null);
keyGenerator.init(new
KeyGenParameterSpec.Builder(KEY_STORE,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
} catch (KeyStoreException | IOException | CertificateException
| NoSuchAlgorithmException | InvalidAlgorithmParameterException
| NoSuchProviderException e) {
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.M)
public boolean cipherInit() {
try {
cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException("Failed to get Cipher", e);
}
try {
keyStore.load(null);
SecretKey key = (SecretKey) keyStore.getKey(KEY_STORE,
null);
cipher.init(Cipher.ENCRYPT_MODE, key);
return true;
} catch (KeyPermanentlyInvalidatedException e) {
return false;
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException("Failed to init Cipher", e);
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private FingerprintManager.AuthenticationCallback authenticationCallback() {
return new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
update("" + errString, false);
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
update("Authentication failed", false);
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
update(helpString.toString(), false);
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
update("Authentication succeeded", true);
}
};
}
private void update(String cudhc, boolean b) {
login_fingerprint_status.setText(cudhc);
if (!b) {
//Authentication faild //Do what you ant to do
} else {
//Authentication Success //Do what you ant to do
}
}
и чем просто вызвать метод CheckRequirementAvailability (), чтобы активировать его
Добавьте это разрешение в манифест
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
Затем все готово, он работает со всеми версиями api и ищет, что он работает в 28+ API.
Помните, старое золото
person
Harsh0021
schedule
21.12.2020