如何在Android FIngerprintManager中以编程方式告知FINGERPRINT_ERROR_LOCKOUT何时到期?

neu*_*an8 6 android android-6.0-marshmallow android-fingerprint-api

当我的应用程序遇到"Too Many Attempts ..."时,身份验证错误0x7,FINGERPRINT_ERROR_LOCKOUT,如何在循环中调用FingerprintManager.authenticate()并获取锁定条件被清除的错误时如何判断?

hop*_*pia 2

查看系统 FingerprintService 的 AOSP 实现,实际上有一个广播 Intent 在锁定期到期后发送出去。要寻找的意图动作是com.android.server.fingerprint.ACTION_LOCKOUT_RESET

在您的 Activity 中,您可以注册一个广播接收器并等待此意图,如下所示:

public class MyActivity extends Activity {
    ...
    private static final String ACTION_LOCKOUT_RESET =
        "com.android.server.fingerprint.ACTION_LOCKOUT_RESET";

    private final BroadcastReceiver mLockoutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ACTION_LOCKOUT_RESET.equals(intent.getAction())) {
                doWhateverYouNeedToDoAfterLockoutHasBeenReset();
            }
        }
    };

    private void registerLockoutResetReceiver() {
        Intent ret = getContext().registerReceiver(mLockoutReceiver, new IntentFilter(ACTION_LOCKOUT_RESET),
                null, null);
    }


    public void onCreate(Bundle savedInstanceState) {
        registerLockoutResetReceiver();
        ...
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

警告:这不是公共 API 的一部分,因此,此行为可能会随着任何后续操作系统更新而改变。但我确实在牛轧糖上尝试过,它对我来说效果很好。

参考:

相关的AOSP代码是./frameworks/base/services/core/java/com/android/server/fingerprint/FingerprintService.java。在这个文件中,我们可以找到一个PendingIntent正在ACTION_LOCKOUT_RESET创建的意图:

private PendingIntent getLockoutResetIntent() {
    return PendingIntent.getBroadcast(mContext, 0,
            new Intent(ACTION_LOCKOUT_RESET), PendingIntent.FLAG_UPDATE_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)

此 PendingIntent 被注册为在一段时间后由 AlarmManager 触发:

private void scheduleLockoutReset() {
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + FAIL_LOCKOUT_TIMEOUT_MS, getLockoutResetIntent());
}
Run Code Online (Sandbox Code Playgroud)