How to Prevent the Lock Screen to automatically lock from the Windows Phone 8 App?

There are times when the automatic lock of the Screen is enabled by the user in the Settings App of the Windows Phone 8. When your App is running on the device, sometimes you may not want to lock the device automatically for various reasons .

How to Prevent the Lock Screen to automatically lock from the Windows Phone 8 App?

Below is a sample code snippet demonstrating how to prevent the system timeout or prevent the Lock Screen to automatically lock from the Windows Phone 8 App.

using Microsoft.Phone.Controls;

using Microsoft.Phone.Tasks;

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using Windows.Phone.Speech.Recognition;

using Windows.System;

namespace AbundantCodeWP8

{

 public partial class MainPage : PhoneApplicationPage

 {

  public MainPage()

  {

   InitializeComponent();

  }

  public void PreventLockScreen(bool value)

  {

   if (value)

    Microsoft.Phone.Shell.PhoneApplicationService.Current.UserIdleDetectionMode = Microsoft.Phone.Shell.IdleDetectionMode.Disabled;

   else

    Microsoft.Phone.Shell.PhoneApplicationService.Current.UserIdleDetectionMode = Microsoft.Phone.Shell.IdleDetectionMode.Enabled;

  }

  private void Button_Click_1(object sender, RoutedEventArgs e)

  {

   PreventLockScreen(true);

  }

 }

}