KeyguardManager - requestDismissKeyguard() method alternative for api 25 and below [Android]
Asked Answered
M

3

10

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 ??

Moreno answered 24/5, 2018 at 11:42 Comment(2)
Please add comments if you guys are downvotingMoreno
Yeah, not sure why the downvotes here, have a +1, friend.Sieve
T
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);
Tatia answered 28/6, 2020 at 11:27 Comment(0)
A
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.

Arginine answered 20/7, 2022 at 8:53 Comment(0)
C
-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();
       }
    }
}
Caughey answered 7/11, 2018 at 17:16 Comment(1)
createConfirmDeviceCredentialIntent is just used to check if user is known with credentials or not, If password is correct it never UN-LOCKS THE PHONEDeclamation

© 2022 - 2024 — McMap. All rights reserved.