Use screen lock in my app
Asked Answered
M

3

24

Is it possible to use the default security settings, which user has set to the phone, as a locking or login mechanism for my app too? I mean like when we reset the phone, it asks for phone password or pattern.

Is it possible the same way that I can use the default android password or pattern set by user to login to my app?

My goal is to bypass the developing effort and use some standard way of authentication without making user to remember another new password.

NOTE: I'm aware that I can lock the screen programmatically. But instead, I want to use the lock as a verification before performing any critical operation. (Just like how Settings ask for the password before resetting the the phone.)

Maharani answered 5/4, 2015 at 10:43 Comment(4)
Did you try this or this or this?Floating
Those all are locking the "device". I dont want to lock the screen completely. Instead, I want to use the lock as a verification before performing any critical operation. (Just like ask the password before reset the phone)Maharani
Short answer: No, not at this moment.Annmaria
@Mangesh: Are you sure that the answer by CommonsWare that you've marked as accepted is actually correct? There's another answer by agi below that says it isn't, and has received a large number of upvotes. I can't really verify it myself, but if agi's answer is indeed correct, you might want to accept it instead.Matriculation
C
31

Actually, there is an API to exactly that using the KeyguardManager.

First get a the Keyguard SystemService:

KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);

And then request an authentication intent using:

Intent i = km.createConfirmDeviceCredentialIntent(title,description);

start this intent using startActivityForResult(Intent, int) and check for RESULT_OK if the user successfully completes the challenge.

This is for API level 21. Previous versions might work with KeyguardLock.

Courtund answered 15/4, 2015 at 12:36 Comment(2)
I am using fingerprint in phone, when Running this code asking for pin. Do you have any solution for this?Sample
@CecilPaul The fingerprint should also work along with the PIN. You can see a fingerprint icon on the lock screen which comes up when you run the intent.Schwaben
H
19

I'm just following @agi with few enhancement,

    public class MainActivity extends AppCompatActivity {
    private static int CODE_AUTHENTICATION_VERIFICATION=241;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
        if(km.isKeyguardSecure()) {

            Intent i = km.createConfirmDeviceCredentialIntent("Authentication required", "password");
            startActivityForResult(i, CODE_AUTHENTICATION_VERIFICATION);
        }
        else
            Toast.makeText(this, "No any security setup done by user(pattern or password or pin or fingerprint", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK && requestCode==CODE_AUTHENTICATION_VERIFICATION)
        {
            Toast.makeText(this, "Success: Verified user's identity", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(this, "Failure: Unable to verify user's identity", Toast.LENGTH_SHORT).show();
        }
    }
}
Helfrich answered 8/11, 2017 at 5:33 Comment(3)
As added by @agi, this follows only for the OS Lollipop and above.Helfrich
To add onActivityResult in kotlin add ? after Intent. otherwise data might be null and it will throw exception. onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {....}Devitalize
@AbhijitKurane what should I do when I want to ask me the password every time I want to open my app?I mean when I temporarily exit the app for checking my email and then return to my app I want to enter the password. I wrote the above code in onResume method but it doesn't work and enter a infinite loopLindholm
C
0

Update 2022,June:(minSdk : 24, targetSdk: 32,compileSdk : 32) Code Enhancement to @Abhijit Kurane's reply...

Replace startActivityForResult with startActivityIfNeeded...(#startActivityForResult is deprecated as of now)

For Beginners : And put finish() inside the else statement of onActivityResult method for not letting the user to open the activity without verification!

Cult answered 30/6, 2022 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.