I'm following this tutorial.Right now, I'm trying to loop the fingerprint authentication part so that I can keep reauthenticate user fingerprint. I've tried to use thread in onStart() and onCreate() to while loop the authentication but the app is stuck in both cases.
Original code that can authenticate only one time
@Override
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);
}
}
Thread in onStart() / onCreate() that failed
@Override
protected void onStart() {
super.onStart();
new Thread(new Runnable(){
public void run() {
while(true)
{
try {
Thread.sleep(50);
if (cipherInit()) {
cryptoObject = new FingerprintManager.CryptoObject(cipher);
FingerprintHandler helper = new FingerprintHandler(MainActivity.this);
helper.startAuth(fingerprintManager, cryptoObject);
}} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}).start();}
Other than using thread, I also tried to use AsyncTask to do the while loop for me. This is my attempt in creating the class. My problem is that the cipherInit() resides in MainActivity.java and how can I invoke the method from my Looping class?
Looping.java
import android.hardware.fingerprint.FingerprintManager;
import android.os.AsyncTask;
public class Looping extends AsyncTask<Object,Void,Void> {
FingerprintManager fingerprintManager;
FingerprintManager.CryptoObject cryptoObject;
Cipher cipher;
@Override
protected Void doInBackground(Void... arg0) {
cipher = (Cipher) arg0[0];
while(true) {
if (cipherInit()) {
cryptoObject = new FingerprintManager.CryptoObject(cipher);
FingerprintHandler helper = new FingerprintHandler(MainActivity.this);
helper.startAuth(fingerprintManager, cryptoObject);
}
}
}}
MainActivity
Looping loop = new Looping();
loop.execute(cipher, null, null);
This is my first personal project and I'm still relatively new with the whole Android structure. I'll really appreciate any input from you all. Thanks in advance