Android Confirm Credential (Security Lock)

Android Confirm Credential (Security Lock)

Hi all today we are going to work on one more app security topic apart from FingerPrint Authentication. In many of apps you have seen the app is opening a screen where it ask you to enter the PIN or draw a Pattern which is by default security you have set to your device. Some the apps are Tez, Paytm, etc.

NOTE : The Security Lock authentication only works for API Level 21+ (Lollipop+). 

So today we are going to do the same and we will see how we can also implement the same kind of authentication which other apps are doing. The steps to implement the Security Level Authentication are following:

1. You will be needing a KeyguardManager to implement this authentication:

2. Create an Intent like below:

After creating Intent start the intent with startActivityForResult() to check if the authentication is successful or not.

It will open the screen requesting user to enter the PIN or draw a Pattern which he has set for particular device.

Unlock via Pattern Screen

Unlock via Pattern Screen

3. Some of the devices have not enabled Security Lock for their device. So for those devices you will get exception and in that case you need to open the Security Settings of the device with an intent:

4. Alternate way to check if the device is Security Lock enabled or not is given below :

The above method only works for API Level 16+ (Jelly Bean+).

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. Now go to activity_main.xml and add the below code to it.

4. After adding the activity_main.xml layout come to MainActivity.java and put the below code to it. This code contains all step by step information which we discussed above.

Unlock via PIN Screen

Unlock via PIN Screen

PIN/Pattern is Authenticated Successfully

PIN/Pattern is Authenticated Successfully

 

5. Finally all done, now you can also make your app secure with the help of Confirm Credential/Security Lock.

USAGE

You can use the above code to authenticate the users while making Payment in your app or while accessing some critical information from app.

Thanks. :)

Download Source Code

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (3)
Reply

Hi
I followed your article closely. Could you extend its value by providing a MainActivity.java that will work with API 16 and above? The article states that some code will only work above API but the MainActivity.java will clearly only work for API 21. I look forward to hearing from you.

    Hi Valentin,

    If you read the article I have already written that this API only work for 21+ . For lower devices this code won’t work. For that you have to create and enable your own lock.

    Thanks

Reply

As you replied to valentin that you have to create your own lock screen activity in that case will you please create post or example as above for the same. Looking forward to help from you.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)