9

I was recently trying to request user to dismiss key-guard manually from my app. My app invokes a activity screen when device is locked and the screen has the flags

FLAG_SHOW_WHEN_LOCKED
FLAG_TURN_SCREEN_ON

When I have to invoke another screen which does not have these flags I want to request the user to unlock the key-guard, this behaviour can be seen in the the camera app - when we want to share a photo taken while phone is locked it will request us to unlock the device.

requestDismissKeyguard() method works only for api26 and above any alternatives for the lower apis ??

Shangeeth Sivan
  • 2,150
  • 1
  • 20
  • 20

3 Answers3

2

you can use flags for lower versions

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
David Buck
  • 3,752
  • 35
  • 31
  • 35
0
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
  val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
  keyguardManager.requestDismissKeyguard(this, null)
} else {
  window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
}

Check for more examples here.

lomza
  • 9,412
  • 15
  • 70
  • 85
-3

you can use createConfirmDeviceCredentialIntent in API level 21

https://developer.android.com/reference/android/app/KeyguardManager.html#createConfirmDeviceCredentialIntent(java.lang.CharSequence,%20java.lang.CharSequence)

sample:

//region [in some funtion]
if (keyguardManager.isKeyguardLocked()) {
   Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("My Title", "A Custom Description");
   if (intent != null) {
       startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
   }else{
        Toast.makeText(MainActivity.this, "Secure lock screen hasn't set up", Toast.LENGTH_SHORT).show();
   }
}
//endregion

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
       if (resultCode == RESULT_OK) {
           Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
       } else {
           Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
       }
    }
}
moonjuice
  • 1
  • 1
  • createConfirmDeviceCredentialIntent is just used to check if user is known with credentials or not, If password is correct it never UN-LOCKS THE PHONE – Rushikant Pawar Feb 28 '20 at 17:08