Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

android - Use System Passcode For Authentication

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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  
        }  
    }  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...