How to find out unlock method programmatically?

1.8k views Asked by At

For the Android OS, I need to find out what the user uses to unlock the device. Be it null, PIN, pattern, fingerprint.

1

There are 1 answers

0
ashkhn On

To Detect if an authenticated fingerprint exists:

FingerprintManagerCompat fingerprintManagerCompat = FingerprintManagerCompat.from(context);

if (fingerprintManagerCompat.isHardwareDetected() && fingerprintManagerCompat.hasEnrolledFingerprints()) { 
    // Device supports fingerprint authentication and has registered a fingerprint     
} 

To use this you also need to add a permission

<uses-permission android:name="android.permission.USE_FINGERPRINT" />

To check if lock pattern is enabled:

ContentResolver cr = getContentResolver();

int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);
// If user has pattern unlock then lockPatternEnable will be 1 else 0

There is no explicit way to check for pin/password as far as I'm aware but you can use KeyGuardManager's isDeviceSecure() method

which returns true if device is secured with a PIN, pattern or password. Coupled with the pattern check you can detect if pin is enabled.

Remember to test for fingerprint first because it requires a pin/password unlock to be set as well