0

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

    }

}
Community
  • 1
  • 1
Cliff
  • 1,468
  • 4
  • 14
  • 18

1 Answers1

1

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for your reply. What do you think about [Services](http://stackoverflow.com/a/6957909/3713720). Is it a better alternative and what is the reason that I should use AsyncTask rather than Services? – Cliff Jul 26 '16 at 20:20
  • They're totally different things, and not necessarily an either/or. A Service is meant for long lived processing shared between activities. An AsyncTask is meant for relatively quick processing on another thread. A service can launch additional threads, but by default is on the UI thread. – Gabe Sechan Jul 26 '16 at 20:22