我正在尝试实现指纹以解锁或删除锁定的视图 指纹身份验证使用传感器现有的已注册指纹来进一步显示内容,但指纹身份验证在正常活动中工作正常,但在自定义视图中不起作用,我也尝试在我的自定义屏幕锁定应用程序中实现它,但它冲突或可能是由于资源不可用面临此错误"FINGERPRINT_ERROR_CANCELED"错误代码 5
我不确定这是否是由于资源不可用,因为同时内置的安全密码/图案锁也使用相同的指纹传感器以及我自己的屏幕锁定或自定义视图
此代码工作正常活动中的代码
fingerprintHandler = new FingerprintHandler();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
keyguardManager =
(KeyguardManager) getSystemService(KEYGUARD_SERVICE);
mFingerprintManager =
(FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
//
// generateKey();
// if (cipherInit()) {
// cryptoObject =
// new FingerprintManager.CryptoObject(cipher);
fingerprintHandler.setOnAuthenticationListener(new FingerprintManager.AuthenticationCallback() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
Toast.makeText(FingerprintNormalActivity.this,
"Authentication errorn" + "Error code" + errorCode + "nError String" + errString,
Toast.LENGTH_LONG).show();
imageViewSmokeImages.setImageResource(R.drawable.fingerprinterror);
tvstatus.setText("Authentication errorn" + "Error code" + errorCode + "nError String" + errString);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {`enter code here`
Toast.makeText(FingerprintNormalActivity.this,
"Authentication helpn" + helpString,
Toast.LENGTH_LONG).show();
imageViewSmokeImages.setImageResource(R.drawable.help);
tvstatus.setText("Authentication helpn" + helpString);
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
Toast.makeText(FingerprintNormalActivity.this,
"Authentication succeeded.",
Toast.LENGTH_LONG).show();
imageViewSmokeImages.setImageResource(R.drawable.fingerprintsuccess);
tvstatus.setText("Authentication Successfull");
finish();
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onAuthenticationFailed() {
Toast.makeText(FingerprintNormalActivity.this,
"Authentication failed.",
Toast.LENGTH_LONG).show();
tvstatus.setText("Authentication failed");
imageViewSmokeImages.setImageResource(R.drawable.fingerprintfailed);
}
});
// new AuthenticateUser();
//// fingerprintHandler.startListening();
if (!getKeyStore())
return;
if (!createNewKey(false)) {
return;
}
if (!getCipher()) {
return;
}
if (!initCipher(Cipher.ENCRYPT_MODE)) {
return;
}
if (!initCryptObject()) {
return;
} else {
fingerprintHandler.startListening(cryptoObject);
}
// }
}
这是我的指纹句柄课程要听
private static FingerprintManager.AuthenticationCallback mAuthenticationCallback;
private static CancellationSignal mCancellationSignal;
private static Context mContext;
@RequiresApi(api = Build.VERSION_CODES.M)
public class FingerprintHandler {
public void setOnAuthenticationListener(FingerprintManager.AuthenticationCallback listener) {
mAuthenticationCallback = listener;
}
public void startListening() {
if (isFingerScannerAvailableAndSet()) {
try {
mCancellationSignal = new CancellationSignal();
mFingerprintManager.authenticate(null, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, new Handler(Looper.getMainLooper()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void startListening(FingerprintManager.CryptoObject cryptoObject) {
if (isFingerScannerAvailableAndSet()) {
try {
mCancellationSignal = new CancellationSignal();
mFingerprintManager.authenticate(cryptoObject, mCancellationSignal, 0 /* flags */, mAuthenticationCallback, new Handler(Looper.getMainLooper()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stopListening() {
if (isFingerScannerAvailableAndSet()) {
try {
mCancellationSignal.cancel();
mCancellationSignal = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean isFingerScannerAvailableAndSet() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return false;
if (ActivityCompat.checkSelfPermission(FingerprintNormalActivity.this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(FingerprintNormalActivity.this, "User hasn't granted permission to use Fingerprint", Toast.LENGTH_LONG).show();
return false;
}
if (mFingerprintManager == null){
Toast.makeText(FingerprintNormalActivity.this,
"mFingerprintManager is null",
Toast.LENGTH_LONG).show();
tvstatus.setText("mFingerprintManager is null");
return false;
}
if (!mFingerprintManager.isHardwareDetected()) {
Toast.makeText(FingerprintNormalActivity.this,
"fingerprint hardware not present or not functional",
Toast.LENGTH_LONG).show();
tvstatus.setText("fingerprint hardware not present or not functional");
return false;
}
if (!mFingerprintManager.hasEnrolledFingerprints()) {
Toast.makeText(FingerprintNormalActivity.this,
"no fingerprint enrolled/saved",
Toast.LENGTH_LONG).show();
tvstatus.setText("no fingerprint enrolled/saved");
return false;
}
return true;
}
}
请参阅此问题。这似乎发生在大多数设备上,需要作为特殊情况处理。似乎对我有用的是停止指纹侦听器此错误,然后重新启动它。
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
if (errMsgId == FingerprintManager.FINGERPRINT_ERROR_CANCELED){
stopListening();
restartListeningToFingerprint();
}
}
在 restartListening(( 方法中,我只是使用一个新的 Cipher 实例调用startListening
。这似乎适用于我拥有的所有设备。但我确实看到一些随机的致命异常,一些三星设备可能是这种情况的副作用。我很想知道其他人如何处理此错误。