【发布时间】:2021-03-31 07:58:56
【问题描述】:
是否可以在不打开系统提供的对话框并在 api 28+ 上弃用 FingerPrintManager 的情况下静默扫描指纹?
【问题讨论】:
标签: android biometrics android-biometric-prompt
是否可以在不打开系统提供的对话框并在 api 28+ 上弃用 FingerPrintManager 的情况下静默扫描指纹?
【问题讨论】:
标签: android biometrics android-biometric-prompt
嘿,
我已经为我做了你所说的,和你分享的时间很简单。
看看就好!!
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 中被破坏。
记住老是金
【讨论】: