Android Confirm Credential (Security Lock) - Androhub

Security Lock Banner

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. :)

 

4 Comments

Valentin Gonzalez
Saturday, March 17th, 2018

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.

Dr. Droid
Saturday, March 17th, 2018

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

vishwajeet
Thursday, March 22nd, 2018

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.

Valentin Gonzalez
Monday, May 21st, 2018

Thank you for the reply.

Separate question on my example:

Every time I called the Screenlock challenge my resultCode is always -1 on valid PIN number.

Is there a reason for createConfirmDeviceCredentialIntent to always return that value? If I intentionally enter an invalid PIN the return value is zero (0).

Do you have an idea why?

Post comment

Your email address will not be published. Required fields are marked *