13
votes

I have written an app that performs some lengthy operations, such as web requests, in a background thread. My problem is that after a while the automatic screen lock turns the screen off and my operations are aborted.

Is there a way to prevent the screen to be automatically turned off during these operations? Or is it in some way possible to keep running while screen is turned off?

I know there are ways to prevent the screen to turn off while debugging, but i need this behavior in the hands of the end user. Therefore I can not rely on some setting being set on the phone, but rather some programmatic solution.

2
@MarcinJuraszek That question addresses another problem while testing and debugging. My problem arises when the end user is using the app. Will update question to be more clear.PKeno

2 Answers

20
votes

The screen can be forced to stay on using the UserIdleDetectionMode property of the current PhoneApplicationService.

To disable automatic screen lock:

PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

To enable it again:

PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;

More information can be found on MSDN

8
votes

I know this question is about Windows Phone 8, but I had a hard time figuring out the way for Windows Phone 8.1 (Universal XAML Apps). Use:

var displayRequest = new Windows.System.Display.DisplayRequest();
displayRequest.RequestActive();

Apps that show video or run for extended periods without user input can request that the display remain on by calling DisplayRequest::RequestActive. When a display request is activated, the device's display remains on while the app is visible. When the user moves the app out of the foreground, the system deactivates the app's display requests and reactivates them when the app returns to the foreground.

See: http://msdn.microsoft.com/en-us/library/windows/apps/br241816.aspx