屏幕关闭时停止监听指纹

19

我的应用程序的用户报告说,在我的应用程序监听指纹验证时(我调用了fingerprintManager.authenticate),当屏幕关闭(通过按设备电源开关按钮)时,无法使用指纹来解锁设备。

我还发现,当屏幕关闭时会调用onAuthenticationError回调方法,而当我离开activity时不会调用该方法,因为我在onPause方法中调用了CancellationSignal.cancel()。我检查过onPause方法确实被调用了。

在指纹对话框示例(https://github.com/xamarin/monodroid-samples/tree/master/android-m/FingerprintDialog,从https://github.com/googlesamples/android-FingerprintDialog迁移而来)中也可以观察到同样的行为。

我应该怎么解决这个问题呢?

编辑:我还尝试注册一个接收器以便于监听android.intent.action.SCREEN_OFF广播(在onPause之后通知它),所以调用那个接收器中的cancel()并没有改变任何东西。


你的onPause方法之前还是之后会出现onAuthenticationError? - user5069935
1
嗨,菲利普,你解决了这个问题吗?如果是的话,能否请分享解决方案,谢谢! - Xcihnegn
3个回答

8

我的问题和你的类似:如果有人通过按下Home键将我的应用程序发送到后台,它仍然控制着指纹传感器,因此其他人无法使用它。从活动的onPause()中调用cancel并不起作用:

    @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() 方法中调用您的 CancellationSignalcancel() 方法,但是要在 super.onPause() 之前。否则,您将收到以下警告:

Rejecting your.package.name. ; not in foreground

cancelAuthentication(): reject your package name

我已经搜索了 Android 指纹服务的源代码,并找到了这些代码行:

 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。而且在调用super.onPause()之后,Android认为我们处于后台状态。经过几个小时的研究,我找到的唯一解决方案是交换取消操作和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 上对我有用。希望这可以帮到你。


2
我曾遇到与三星指纹SDK类似的问题(当屏幕锁定或关闭时无法进行身份验证,这不是错误,而是设计如此)。经过审查后,我们得出结论,最好的方法是为用户创建一个通知,其中包含一个PendingIntent,该PendingIntent将触发您的应用程序并启动指纹验证过程。
通知可以使手机振动/哔哔声,以便提醒用户。
希望这可以帮助你。

我不认为这对我的情况有帮助。我说的是当我已经开始认证,但用户关闭了屏幕的情况。 - Philipp

2
在回调方法onError()中,也调用'stopListeningc'。然后当屏幕关闭时,您应用程序中的指纹监听器将停止工作,然后您的设备将监听指纹。
希望这可以帮到你!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接