user5994722
user5994722

Reputation: 21

Detect when screen is locked UWP

How can I detect if the screen is locked in UWP apps?

When screen is locked, suspension of app occurs and I need to put some different code in case of locking screen.

Upvotes: 0

Views: 1660

Answers (2)

Ang3lCandi
Ang3lCandi

Reputation: 11

You will find your answer here: https://developerinsider.co/prevent-the-screen-from-locking-on-uwp/

For those that are not looking for a link to another post that could, or could not, potentially be helpful here is the summary

The code you would use is

var displayRequest = new DisplayRequest();
displayRequest.RequestActive(); //to request keep display on     
displayRequest.RequestRelease(); //to release request of keep display on

Things to keep in mind are

  1. Use display requests only when required, that is, times when no user input is expected but the display should remain on. For example, during full screen presentations or when the user is reading an e-book.
  2. Release each display request as soon as it is no longer required.
  3. Release all display requests when the app is suspended. If the display is still required to remain on, the app can create a new display request when it is reactivated.

Upvotes: 1

Tom Droste
Tom Droste

Reputation: 1324

No, you can't exactly detect when a user activates the lockscreen, but you can detect changes in the lifecycle. This way you can detect when a user suspends your app.

For information about the lifecycle: https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle

Upvotes: 0

Related Questions