android - 屏幕关闭时停止监听指纹

标签 android fingerprint

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

我还可以看到onAuthenticationError回调方法是在屏幕关闭时调用的,当我离开我的 Activity 时不会发生这种情况,因为我调用了CancellationSignal.cancel()在我的onPause方法。我已经检查了 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() 也就不足为奇了。在那个接收器不会改变任何东西。

最佳答案

我的问题与您的问题相似:如果有人通过按主页按钮将我的应用程序发送到后台,它仍然控制指纹传感器,因此其他人无法使用它。从 Activity 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();
            } 
    }

您需要调用cancel()你的方法CancellationSignal在您的 Activity 中onPause()方法但之前 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。 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 上为我工作。希望这会有所帮助。

关于android - 屏幕关闭时停止监听指纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34609427/

相关文章:

android - 将 onclick 监听器添加到预定义按钮?

Linux,su,在远程 session 上禁用指纹验证

python - 如何使用传感器 R305 搜索已保存的指纹?

Android - 指纹信息存储在设备中的位置和安全程度

Android:如何更改 JellyBean 中的状态栏颜色(API 18)

java - Android:如何在另一个包的 Activity 中显示异步任务的进度对话框?

c# - 使用 Windows 生物识别框架或任何其他方法通过 .Net 访问指纹读取器

ubuntu - gpg : WARNING: unsafe ownership on configuration file, $gpg --Ubuntu9.10 上的指纹

Android - 仅在智能手机中强制方向

java - MotionEvent.ACTION_UP 未被调用