7

I cannot find an API that will allow me to tap into the password/pin/pattern the user has set up on an Android device for authentication in my app.

There is a Fingerprint Authentication API that I can use to ask Android to authenticate a user, but there is no passcode API to tap into the password/pin/pattern instead of fingerprint.

Is there a way to ask Android to authenticate in my app based upon the password/pin/pattern the user already has set up?

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
D. Wall
  • 77
  • 1
  • 5

1 Answers1

26

If you are asking "how can I ask the system to re-authenticate the user?", on Android 5.0+, something like this should work:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

    if (km.isKeyguardSecure()) {
        Intent authIntent = km.createConfirmDeviceCredentialIntent(getString(R.string.dialog_title_auth), getString(R.string.dialog_msg_auth));
        startActivityForResult(authIntent, INTENT_AUTHENTICATE);
    }
}

You can see that code in use (with slight modifications) in the andOTP project.

If you are asking "how can I get the user's password/pin/pattern for my own use?", that is not possible.

Use this to see if the user successfully authenticated:

// call back when password is correct  
@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == INTENT_AUTHENTICATE) {  
        if (resultCode == RESULT_OK) {  
            //do something you want when pass the security  
        }  
    }  
}
D. Wall
  • 77
  • 1
  • 5
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, this seems to be on the right track. Really I'm just looking for a pass or fail. I just want Android to let me know whether the user successfully confirmed credentials or if they failed. Does this have a callback that lets me know if they pass or fail. Looking at it now, but I do not see a callback to see the result. – D. Wall Aug 16 '17 at 17:07
  • It looks like this post answers that. Checking it out now. https://stackoverflow.com/questions/39202487/can-app-use-android-security-mechanism-pin-code-pattern-password – D. Wall Aug 16 '17 at 17:10
  • 1
    Yep. That works. I'll update your answer with the callback and mark this as the correct answer. Thank you for the help!! – D. Wall Aug 16 '17 at 17:13
  • 1
    @CommonsWare Is there any alternative to do the same for below 5 versions? – Sarath Kn Jul 20 '18 at 05:53
  • @Srt: I am not aware of anything, sorry. – CommonsWare Jul 20 '18 at 11:56