UWP: Keeping the display alive

There are some scenarios when creating apps where you want to or need to have your app running in the foreground without your phone or tablet going into “sleep” after a short while.

For instance; the other day I celebrated my birthday and I wanted to stream Spotify to my TV to have some background music for the party, but I currently do not have any media capabilities other than my Xbox One. Unfortunately Spotify does not have an app for XB just yet, that I am aware of.

What I do happen to have is the Microsoft Display Dock and a Lumia 950 XL at hand. So, 45 minutes before the guests arrived I connected my phone and the dock to my TV and started playing some music. All is good, but after a few minutes the display on my phone shut down and with it the connection to the TV. Running Continuum — which is what Microsoft calls the experience — requires the phone or the display to be active at all times.

What’s peculiar is that there is no setting that I could find that would allow me to keep the display on when connected to the Display Dock which is a bit weird, or I might just not be that good at looking.

As a software developer I took the challenge to develop an UWP app that would keep the phone’s display active.

Googling how to achieve this I stumbled on to the DisplayRequest class which allows one to request the display to stay alive using the RequestActivemethod:

1

2

vardisplayRequest=newWindows.System.Display.DisplayRequest();

displayRequest.RequestActive();

From the MSDN documentation it says you should release all display requests when not needed anymore using the RequestReleasemethod:

1

displayRequest.RequestRelease();

From the examples and the documentation it looks like it should only be necessary to call the RequestActive method once and then release it when done. However, when I ran the app on my phone it would still shut down after about 15 minutes. Now, I must add that I run the latest and greatest Windows 10 Insider Preview build on the Fast Ring so it might be related to an issue or bug in the OS.

The solution to my problem was to add a timer and every few minutes I would request the display to be active. This worked for me and I got it working just in time for the party. Phew. Thug life.

Now, since I already had the Continuum experience up and running with the app keeping the TV and my phone’s display active I thought I could extend the app to show some content on the TV instead of it just showing my background. Check out our post on how to project content onto a secondary display here!