0

这个基本的 Android 教程应用程序将打印一条验证指纹认证的 Toast 消息。但它只能验证一次。我希望它能够在应用程序仍在运行时重新验证指纹。我试图在 helper.startAuth() 周围添加一个 while 循环,但它不起作用。我已经引用了几个问题(123),但没有一个对我有帮助。这是我尝试过的,但它不起作用。

if (cipherInit()) {
    cryptoObject = new FingerprintManager.CryptoObject(cipher);
    FingerprintHandler helper = new FingerprintHandler(this);
    while(true){
        helper.startAuth(fingerprintManager, cryptoObject);
    }
}

这是我的 onCreate()。谢谢大家的帮助和指导

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

    if (!keyguardManager.isKeyguardSecure()){
        Toast.makeText(this,
                "Lock screen security is not enable in Settings", Toast.LENGTH_LONG).show();
        return;
    }

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED){
        Toast.makeText(this,
                "Fingerprint authentication permission is not enabled", Toast.LENGTH_LONG).show();
        return;
    }

    if (!fingerprintManager.hasEnrolledFingerprints()){
        Toast.makeText(this, "Register at least one fingerprint in Settings", Toast.LENGTH_LONG).show();
        return;
    }

    generateKey();
    if (cipherInit()) {
        cryptoObject = new FingerprintManager.CryptoObject(cipher);
        FingerprintHandler helper = new FingerprintHandler(this);
        helper.startAuth(fingerprintManager, cryptoObject);

    }

}
4

1 回答 1

1

你不能在 onCreate 中重复做任何事情。OnCreate 需要完成并继续,否则应用程序将被看门狗杀死。事实上,你永远不应该在 UI 线程上重复做任何事情。您要么需要在另一个线程(或 AsyncTask)上执行此操作,要么需要在某种计时器上执行此操作。

于 2016-07-26T18:01:41.490 回答