简体   繁体   中英

Method failed to loop in onStart() and onCreate() thread

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

You should not need the secondary thread or loop to do the authentication. The call to FingerprintManager.authenticate() is done in your FingerprintHandler (assuming you have the same code as the tutorial you cited). This is an asyc operation and the handler ( FingerprintManager.AuthentciationCallback ) is called back when the auth succeeds or fails. You need to take action based on that success/failure rather than poll in a while loop. That callback will occur on your main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM