我的问题类似于你的:如果有人通过按Home按钮将我的应用程序发送到背景,它仍然控制指纹传感器,所以没有人可以使用它。呼叫取消活动onPause()不工作:
@Override
protected void onPause() {
super.onPause();
/**
* We are cancelling the Fingerprint Authentication
* when application is going to background
*/
if (fingerprintHelper!=null && fingerprintHelper instanceof AndroidFingerprintHelper){
log.info("canceling AndroidFingerprintHelper dialog");
fingerprintHelper.cancelIdentify();
}
}
您需要在活动的onPause()方法中调用cancelationSignal的cancel()方法,但在super.onPause()之前调用。否则你会得到警告
Rejecting your.package.name. ; not in foreground
cancelAuthentication(): reject your package name
我搜索了Android FingerPrint服务的源代码,我发现这一行:
public void cancelAuthentication(final IBinder token, String opPackageName) {
if (!canUseFingerprint(opPackageName, false /* foregroundOnly */)) {
return;
}
//we don't get here
...
...
}
其中canUseFingerprint实际上是检查我们是前景还是否(其中一件事情):
private boolean canUseFingerprint(String opPackageName, boolean foregroundOnly) {
if (foregroundOnly && !isForegroundActivity(uid, pid)) {
Slog.v(TAG, "Rejecting " + opPackageName + " ; not in foreground");
return false;
}
//we don't get here
}
这样我们永远不能从后台调用cancelAuth。 Android认为我们是在super.onPause()之后的背景。叫做。经过几个小时的研究,我发现唯一的解决方案是交换取消动作和super.onPause():
@Override
protected void onPause() {
/**
* We are cancelling the Fingerprint Authentication
* when application is going to background
*/
if (fingerprintHelper!=null && fingerprintHelper instanceof AndroidFingerprintHelper){
log.info("canceling AndroidFingerprintHelper dialog");
fingerprintHelper.cancelIdentify();
}
super.onPause();
}
在Android M和N上为我工作。希望这有帮助。