KeyguardManager  |  API reference  |  Android Developers (original) (raw)


public class KeyguardManager
extends [Object](/reference/java/lang/Object) ``


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

Summary

Nested classes
interface KeyguardManager.DeviceLockedStateListener Listener for device locked state changes.
class KeyguardManager.KeyguardDismissCallback Callback passed toKeyguardManager.requestDismissKeyguard(Activity,KeyguardDismissCallback) to notify caller of result.
class KeyguardManager.KeyguardLock This class was deprecated in API level 15. Use R.attr.showWhenLocked or 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.
interface KeyguardManager.KeyguardLockedStateListener Listener for keyguard locked state changes.
interface KeyguardManager.OnKeyguardExitResult This interface was deprecated in API level 26. Use KeyguardDismissCallback
Public methods
void addDeviceLockedStateListener(Executor executor, KeyguardManager.DeviceLockedStateListener listener) Registers a listener to execute when the device locked state changes.
void addKeyguardLockedStateListener(Executor executor, KeyguardManager.KeyguardLockedStateListener listener) Registers a listener to execute when the keyguard locked state changes.
Intent createConfirmDeviceCredentialIntent(CharSequence title, CharSequence description) This method was deprecated in API level 29. see BiometricPrompt.Builder.setAllowedAuthenticators(int)
void exitKeyguardSecurely(KeyguardManager.OnKeyguardExitResult callback) This method was deprecated in API level 15. Use R.attr.showWhenLocked or 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,KeyguardDismissCallback) to request dismissal of the keyguard.
boolean inKeyguardRestrictedInputMode() This method was deprecated in API level 28. Use isKeyguardLocked() instead.
boolean isDeviceLocked() Returns whether the device is currently locked for the user.
boolean isDeviceSecure() Returns whether the user has a secure lock screen.
boolean isKeyguardLocked() Returns whether the lock screen (also known as Keyguard) is showing.
boolean isKeyguardSecure() Returns whether the user has a secure lock screen or there is a locked SIM card.
KeyguardManager.KeyguardLock newKeyguardLock(String tag) This method was deprecated in API level 15. Use R.attr.showWhenLocked or 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.
void removeDeviceLockedStateListener(KeyguardManager.DeviceLockedStateListener listener) Unregisters a listener that executes when the device locked state changes.
void removeKeyguardLockedStateListener(KeyguardManager.KeyguardLockedStateListener listener) Unregisters a listener that executes when the keyguard locked state changes.
void requestDismissKeyguard(Activity activity, KeyguardManager.KeyguardDismissCallback callback) Requests that the Keyguard (lock screen) be dismissed if it is currently showing.
Inherited methods
From class java.lang.Object Object clone() Creates and returns a copy of this object. boolean equals(Object obj) Indicates whether some other object is "equal to" this one. void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. finalClass<?> getClass() Returns the runtime class of this Object. int hashCode() Returns a hash code value for the object. final void notify() Wakes up a single thread that is waiting on this object's monitor. final void notifyAll() Wakes up all threads that are waiting on this object's monitor. String toString() Returns a string representation of the object. final void wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public methods

addDeviceLockedStateListener

public void addDeviceLockedStateListener (Executor executor, KeyguardManager.DeviceLockedStateListener listener)

Registers a listener to execute when the device locked state changes.
Requires [Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE](/reference/android/Manifest.permission#SUBSCRIBE%5FTO%5FKEYGUARD%5FLOCKED%5FSTATE)

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

addKeyguardLockedStateListener

public void addKeyguardLockedStateListener (Executor executor, KeyguardManager.KeyguardLockedStateListener listener)

Registers a listener to execute when the keyguard locked state changes.
Requires [Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE](/reference/android/Manifest.permission#SUBSCRIBE%5FTO%5FKEYGUARD%5FLOCKED%5FSTATE)

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

exitKeyguardSecurely

public void exitKeyguardSecurely (KeyguardManager.OnKeyguardExitResult callback)

This method was deprecated in API level 15.
Use [R.attr.showWhenLocked](/reference/android/R.attr#showWhenLocked) or [Activity.setShowWhenLocked(boolean)](/reference/android/app/Activity#setShowWhenLocked%28boolean%29) 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,KeyguardDismissCallback)](/reference/android/app/KeyguardManager#requestDismissKeyguard%28android.app.Activity,%20android.app.KeyguardManager.KeyguardDismissCallback%29) 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 [Manifest.permission.DISABLE_KEYGUARD](/reference/android/Manifest.permission#DISABLE%5FKEYGUARD)

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

public boolean inKeyguardRestrictedInputMode ()

This method was deprecated in API level 28.
Use [isKeyguardLocked()](/reference/android/app/KeyguardManager#isKeyguardLocked%28%29) instead.

Returns whether the lock screen is showing.

This is exactly the same as [isKeyguardLocked()](/reference/android/app/KeyguardManager#isKeyguardLocked%28%29).

Returns
boolean the value of isKeyguardLocked()

isDeviceLocked

public boolean isDeviceLocked ()

Returns whether the device is currently locked for the user.

This method returns the device locked state for the [Context](/reference/android/content/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.

Note: In the case of multiple full users, each user can have their own lock screen authentication configured. The device-locked state may differ between different users. For example, the device may be unlocked for the current user, but locked for a non-current user if lock screen authentication would be required to access that user's apps after switching to that user.

In the case of a profile, when the device goes to the main lock screen, up to two layers of authentication may be required to regain access to the profile's apps: one to unlock the main lock screen, and one to unlock the profile (when a separate profile challenge is required). For a profile, the device is considered to be locked as long as any challenge remains, either the parent user's challenge (when applicable) or the profile's challenge (when applicable).

Returns
boolean true if the device is currently locked for the user

isDeviceSecure

public boolean isDeviceSecure ()

Returns whether the user has a secure lock screen.

This returns true if the [Context](/reference/android/content/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()](/reference/android/app/KeyguardManager#isKeyguardSecure%28%29) which includes locked SIM cards.

Returns
boolean true if the user has a secure lock screen

isKeyguardLocked

public boolean isKeyguardLocked ()

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

Specifically, this returns true in the following cases:

"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](/reference/android/content/Context)'s user specifically.

Note that isKeyguardLocked() is confusingly named and probably should be calledisKeyguardShowing(). 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()](/reference/android/app/KeyguardManager#isDeviceLocked%28%29) is an alternative API that has slightly different semantics.

Returns
boolean true if the lock screen is showing

isKeyguardSecure

public boolean isKeyguardSecure ()

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:

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

See also [isDeviceSecure()](/reference/android/app/KeyguardManager#isDeviceSecure%28%29) which excludes locked SIM cards.

Returns
boolean true if the user has a secure lock screen or there is a locked SIM card

newKeyguardLock

public KeyguardManager.KeyguardLock newKeyguardLock (String tag)

This method was deprecated in API level 15.
Use [R.attr.showWhenLocked](/reference/android/R.attr#showWhenLocked) or [Activity.setShowWhenLocked(boolean)](/reference/android/app/Activity#setShowWhenLocked%28boolean%29) 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).
Returns
KeyguardManager.KeyguardLock A KeyguardLock handle to use to disable and reenable the keyguard.

requestDismissKeyguard

public void requestDismissKeyguard (Activity activity, KeyguardManager.KeyguardDismissCallback callback)

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](/reference/android/app/Activity) attr [R.attr.turnScreenOn](/reference/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 R.attr.showWhenLocked or 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 andKeyguardDismissCallback.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.