Cliff
Cliff

Reputation: 1568

How To Repeatedly Authenticate FingerPrint in onCreate()

This basic Android tutorial app is going to print a Toast message verifying the fingerprint authentication. But it is only able to authenticate once. I want it to be able to reauthenticate fingerprint whenever the app is still running. I've tried to add a while loop wrapping around the helper.startAuth() and it is not working. I've referenced several questions (1,2,3) but none of those are helping me. This is what I've tried and it is not working.

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

This is my onCreate(). Thank you for all your help and guidance

    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);

    }

}

Upvotes: 0

Views: 208

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93688

You can't repeatedly do anything in onCreate. OnCreate needs to finish and move on, if not the app will be killed by the watchdog. In fact you should never be repeatedly doing anything on the UI thread. You either need to do this on another thread (or AsyncTask) or on a timer of some sort.

Upvotes: 1

Related Questions