Added in API level 1

KeyguardManager


open class KeyguardManager
kotlin.Any
   ↳ android.app.KeyguardManager

Class to manage and query the state of the lock screen (also known as Keyguard).

Summary

Nested classes
abstract

Callback passed to KeyguardManager#requestDismissKeyguard(Activity, KeyguardDismissCallback) to notify caller of result.

open

Handle returned by KeyguardManager#newKeyguardLock that allows you to temporarily disable / reenable the keyguard (lock screen).

abstract

Listener for keyguard locked state changes.

abstract

Callback passed to KeyguardManager#exitKeyguardSecurely to notify caller of result.

Public methods
open Unit

Registers a listener to execute when the keyguard locked state changes.

open Intent!

Get an intent to prompt the user to confirm credentials (pin, pattern, password or biometrics if enrolled) for the current user of the device.

open Unit

Exit the keyguard securely.

open Boolean

Returns whether the lock screen is showing.

open Boolean

Returns whether the device is currently locked for the user.

open Boolean

Returns whether the user has a secure lock screen.

open Boolean

Returns whether the lock screen (also known as Keyguard) is showing.

open Boolean

Returns whether the user has a secure lock screen or there is a locked SIM card.

open KeyguardManager.KeyguardLock!

Enables you to temporarily disable / reenable the keyguard (lock screen).

open Unit

Unregisters a listener that executes when the keyguard locked state changes.

open Unit

Requests that the Keyguard (lock screen) be dismissed if it is currently showing.

Public methods

addKeyguardLockedStateListener

Added in API level 33
open fun addKeyguardLockedStateListener(
    executor: Executor,
    listener: KeyguardManager.KeyguardLockedStateListener
): Unit

Registers a listener to execute when the keyguard locked state changes.
Requires android.Manifest.permission#SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE

Parameters
listener KeyguardManager.KeyguardLockedStateListener: The listener to add to receive keyguard locked state changes. This value cannot be null.
executor Executor: This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.

createConfirmDeviceCredentialIntent

Added in API level 21
Deprecated in API level 29
open fun createConfirmDeviceCredentialIntent(
    title: CharSequence!,
    description: CharSequence!
): Intent!

Deprecated: see android.hardware.biometrics.BiometricPrompt.Builder#setAllowedAuthenticators(int)

Get an intent to prompt the user to confirm credentials (pin, pattern, password or biometrics if enrolled) for the current user of the device. The caller is expected to launch this activity using android.app.Activity#startActivityForResult(Intent, int) and check for android.app.Activity#RESULT_OK if the user successfully completes the challenge.
Requires the PackageManager#FEATURE_SECURE_LOCK_SCREEN feature which can be detected using PackageManager.hasSystemFeature(String).

Return
Intent! the intent for launching the activity or null if no password is required.

exitKeyguardSecurely

Added in API level 1
Deprecated in API level 15
open fun exitKeyguardSecurely(callback: KeyguardManager.OnKeyguardExitResult!): Unit

Deprecated: Use android.R.attr#showWhenLocked or android.app.Activity#setShowWhenLocked(boolean) to seamlessly occlude and unocclude the keyguard as your application moves in and out of the foreground, without requiring any special permissions. Use requestDismissKeyguard(android.app.Activity,android.app.KeyguardManager.KeyguardDismissCallback) to request dismissal of the keyguard.

Exit the keyguard securely. The use case for this api is that, after disabling the keyguard, your app, which was granted permission to disable the keyguard and show a limited amount of information deemed safe without the user getting past the keyguard, needs to navigate to something that is not safe to view without getting past the keyguard. This will, if the keyguard is secure, bring up the unlock screen of the keyguard.
Requires android.Manifest.permission#DISABLE_KEYGUARD

Parameters
callback KeyguardManager.OnKeyguardExitResult!: Lets you know whether the operation was successful and it is safe to launch anything that would normally be considered safe once the user has gotten past the keyguard.

inKeyguardRestrictedInputMode

Added in API level 1
Deprecated in API level 28
open fun inKeyguardRestrictedInputMode(): Boolean

Deprecated: Use isKeyguardLocked() instead.

Returns whether the lock screen is showing.

This is exactly the same as isKeyguardLocked().

Return
Boolean the value of isKeyguardLocked()

isDeviceLocked

Added in API level 22
open fun isDeviceLocked(): Boolean

Returns whether the device is currently locked for the user.

This method returns the device locked state for the Context's user. The device is considered to be locked for a user when the user's apps are currently inaccessible and some form of lock screen authentication is required to regain access to them. The lock screen authentication typically uses PIN, pattern, password, or biometric. Some devices may support additional methods, such as unlock using a paired smartwatch. "Swipe" does not count as authentication; if the lock screen is dismissible with swipe, for example due to the lock screen being set to Swipe or due to the device being kept unlocked by being near a trusted bluetooth device or in a trusted location, the device is considered unlocked.

Return
Boolean true if the device is currently locked for the user

isDeviceSecure

Added in API level 23
open fun isDeviceSecure(): Boolean

Returns whether the user has a secure lock screen.

This returns true if the Context's user has a secure lock screen. A full user or a profile that uses a separate challenge has a secure lock screen if its lock screen is set to PIN, pattern, or password, as opposed to swipe or none. A profile that uses a unified challenge is considered to have a secure lock screen if and only if its parent user has a secure lock screen.

This method does not consider whether the lock screen is currently showing or not.

See also isKeyguardSecure() which includes locked SIM cards.

Return
Boolean true if the user has a secure lock screen

isKeyguardLocked

Added in API level 16
open fun isKeyguardLocked(): Boolean

Returns whether the lock screen (also known as Keyguard) is showing.

Specifically, this returns true in the following cases:

  • The lock screen is showing in the foreground.
  • The lock screen is showing, but it is occluded by an activity that is showing on top of it. A common example is the phone app receiving a call or making an emergency call.
  • The lock screen was showing but is temporarily disabled as a result of lock task mode or an app using the deprecated KeyguardLock API.

"Showing" refers to a logical state of the UI, regardless of whether the screen happens to be on. When the power button is pressed on an unlocked device, the lock screen starts "showing" immediately when the screen turns off.

This method does not distinguish a lock screen that is requiring authentication (e.g. with PIN, pattern, password, or biometric) from a lock screen that is trivially dismissible (e.g. with swipe). It also does not distinguish a lock screen requesting a SIM card PIN from a normal device lock screen. Finally, it always returns the global lock screen state and does not consider the Context's user specifically.

Note that isKeyguardLocked() is confusingly named and probably should be called isKeyguardShowing(). On many devices, the lock screen displays an unlocked padlock icon when it is trivially dismissible. As mentioned above, isKeyguardLocked() actually returns true in this case, not false as might be expected. isDeviceLocked() is an alternative API that has slightly different semantics.

Return
Boolean true if the lock screen is showing

isKeyguardSecure

Added in API level 16
open fun isKeyguardSecure(): Boolean

Returns whether the user has a secure lock screen or there is a locked SIM card.

Specifically, this returns true if at least one of the following is true:

  • The Context's user has a secure lock screen. A full user or a profile that uses a separate challenge has a secure lock screen if its lock screen is set to PIN, pattern, or password, as opposed to swipe or none. A profile that uses a unified challenge is considered to have a secure lock screen if and only if its parent user has a secure lock screen.
  • At least one SIM card is currently locked and requires a PIN.

This method does not consider whether the lock screen is currently showing or not.

See also isDeviceSecure() which excludes locked SIM cards.

Return
Boolean true if the user has a secure lock screen or there is a locked SIM card

newKeyguardLock

Added in API level 1
Deprecated in API level 15
open fun newKeyguardLock(tag: String!): KeyguardManager.KeyguardLock!

Deprecated: Use android.R.attr#showWhenLocked or android.app.Activity#setShowWhenLocked(boolean) instead. This allows you to seamlessly occlude and unocclude the keyguard as your application moves in and out of the foreground and does not require that any special permissions be requested.

Enables you to temporarily disable / reenable the keyguard (lock screen).

Parameters
tag String!: A tag that informally identifies who you are (for debugging who is disabling the keyguard).
Return
KeyguardManager.KeyguardLock! A KeyguardLock handle to use to disable and reenable the keyguard.

removeKeyguardLockedStateListener

Added in API level 33
open fun removeKeyguardLockedStateListener(listener: KeyguardManager.KeyguardLockedStateListener): Unit

Unregisters a listener that executes when the keyguard locked state changes.
Requires android.Manifest.permission#SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE

Parameters
listener KeyguardManager.KeyguardLockedStateListener: The listener to remove. This value cannot be null.

requestDismissKeyguard

Added in API level 26
open fun requestDismissKeyguard(
    activity: Activity,
    callback: KeyguardManager.KeyguardDismissCallback?
): Unit

Requests that the Keyguard (lock screen) be dismissed if it is currently showing.

If the Keyguard is not secure or the device is currently in a trusted state, calling this method will immediately dismiss the Keyguard without any user interaction.

If the Keyguard is secure and the device is not in a trusted state, this will bring up the UI so the user can enter their credentials.

If the value set for the Activity attr android.R.attr#turnScreenOn is true, the screen will turn on when the keyguard is dismissed.

Parameters
activity Activity: The activity requesting the dismissal. The activity must either be visible by using android.R.attr#showWhenLocked or android.app.Activity#setShowWhenLocked(boolean), or must be in a state in which it would be visible if Keyguard would not be hiding it. If that's not the case, the request will fail immediately and KeyguardDismissCallback#onDismissError will be invoked. This value cannot be null.
callback KeyguardManager.KeyguardDismissCallback?: The callback to be called if the request to dismiss Keyguard was successful or null if the caller isn't interested in knowing the result. The callback will not be invoked if the activity was destroyed before the callback was received.