Search code examples
androidandroid-intentandroid-6.0-marshmallowandroid-fingerprint-api

How to get results for fingerprint authentication in previous activity?


I've been working at this for quite awhile and can't figure it out, even though I thought it would be relatively easy. I am using Android's FingerprintManager to authenticate users via their fingerprint and it has worked very well. The issue is that I can't get the authentication results back from my helper class and it is very frustrating. I am pretty new to using helper classes and after googling it extensively, I still can't get the authentication results back into my activity. My best hope was to use intents with onActivityResult, however the helper class never sends out the intent (or atleast my main activity does not receive it).

For reference, my main class is an activity that uses a helper class (which extends .AuthenticationCallback) to check their fingerprints. The helper class contains the following methods:

        public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {

        cancellationSignal = new CancellationSignal();
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
    }

    @Override
    public void onAuthenticationError(int errMsgId, CharSequence errString) {

    }

    @Override
    public void onAuthenticationFailed() {

    }

    @Override
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
    }

    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        Toast.makeText(context, "You have Been Successfully authenticated!", Toast.LENGTH_SHORT).show();
    }

The code from the activity simply sends out the intent and has an onActivityResult method:

        Intent btauthenticate = new Intent(getApplicationContext(), FingerPrintMainActivity.class);
    startActivityForResult(authenticate, 1);

The fingerprints are authenticated successfully and I get my toast message, however I just don't know how to get the results back. I figure there must be an easy way to get the results back, but I just can't seem to figure it out and it's very frustrating. Any help is much appreciated.


Solution

  • Finally found a solution that worked for me. I never really found out why onActivityResult() never worked, even though it works in my other various activities. Best guess is that onActivityResult was meant to work between two activities (I've been trying with one activity and a helper class). I figure there is a way to make it work, but I was not able to figure it out. Maybe it has something to do with not launching it from the activity itself.

    My solution was to simply to use a Handler to process the data, then send an Intent bundle back to the original activity to do its processing.

    For reference to anyone who comes across this thread in the future:

    Helper class contains in the onAuthenticationSucceeded method contains:

            String message = "success" ;
            Message msg = Message.obtain();
            msg.obj = message;
            msg.setTarget(handler1); 
            msg.sendToTarget(); 
    

    Helper class contains this at the beginning of class:

         Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String message = (String) msg.obj;
            Log.i(TAG, "Value of message: " + message);
    
            if (message == "success") {
    
                Intent intent = new Intent(....this, ....class);
                intent.putExtra("message", message);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
    
            }
    
        }
      };
    

    The activity that it launches gets the bundle in its onCreate() then does its processing:

        Bundle bundle = getIntent().getExtras();
        String message = bundle.getString("message");
        if(message...) {
        ....Do Something
        }
    

    I'm sure this isn't the most efficient solution but it works. The nice part to all of this is that the helper class can be turned into a service and used from various other activities. The activities just need to be able to receive the bundle and use basic logic to process further.