Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent the screen from locking on UWP 10

I want to prevent the phone to lock if the user didnt interact with the phone for some time. In win8 phone development i used the PhoneApplicationService.UserIdleDetectionMode Property. Unfortunately i cannot find anything alike for win 10 universal app. Any suggestions?

like image 480
Luinil Avatar asked Dec 01 '22 13:12

Luinil


1 Answers

Simple Answer

DisplayRequest class

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

Detailed Answer

Using display requests to keep the display on consumes a lot of power. Use these guidelines for best app behavior when using display requests.

  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.

To Request keep display on

private void Activate_Click(object sender, RoutedEventArgs e) 
{ 
    if (g_DisplayRequest == null) 
    { 
        g_DisplayRequest = new DisplayRequest(); 
    }
    if (g_DisplayRequest != null) 
    { 
        // This call activates a display-required request. If successful,  
        // the screen is guaranteed not to turn off automatically due to user inactivity. 
        g_DisplayRequest.RequestActive(); 
        drCount += 1; 
    } 
}

To release request of keep display on

private void Release_Click(object sender, RoutedEventArgs e) 
{ 
    // This call de-activates the display-required request. If successful, the screen 
    // might be turned off automatically due to a user inactivity, depending on the 
    // power policy settings of the system. The requestRelease method throws an exception  
    // if it is called before a successful requestActive call on this object. 
    if (g_DisplayRequest != null) 
    {
        g_DisplayRequest.RequestRelease(); 
        drCount -= 1; 
    }
} 

References - Prevent the screen from locking on Universal Windows Platform

Hope it help someone!!

like image 51
Vineet Choudhary Avatar answered Jun 12 '23 11:06

Vineet Choudhary