In this article we will discuss how to disable autolocking of screen. In certain scenarios of application where you don't want the screen to be autolocked while application is running. So the autolocking of screen should be disable when the application launching or activating and autolocking should be enabled when the application deactivating or closing. The downside of disabling autoscreen is that it will continue to run and consume battery even if the application is not used. Still if in some application you need to disable autolocking of screen here is the way to implement it.
Step 1:Open App.xaml and you will get below code.
<shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated"/>
PhoneApplicationService object is created by App.xaml and assigns event handlers to four events.
The Launching event triggers when the program is first executed from start screen.
The Closing event triggers when the program is really terminated, like pressing back button.
The Activated event triggers when the program is restored from tombstoning.
The Deactivated event triggers when the program is tombstoned, like pressing of window button and press and hold the back button.
Step 2: Open App.xaml.cs file and you will get Application_Launching, Application_Activated, Application_Deactivated and Application_Closing methods.
In this events I have put message box to each event to find out when particular event triggers.
Microsoft.Phone.Shell directive has PhoneApplicationService class which has UserIdleDetectionMode property of current application which can be set using IdleDetectionMode enum. IdleDetectionMode enum contains Disable and Enabled.
When IdleDetectionMode is set to disabled the OS will not consider an application to be idle when no touch events are detected.
We need to disable the autolocking of screen when the application is launching or activating using below line in Application_Launching and Application_Activated methods
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
We need to enable the autolocking of screen when the application is deactivating or closing using below line in Application_Deactivated and Application_Closing methods.
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
// Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e) { MessageBox.Show("Launching"); PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; }
// Code to execute when the application is activated (brought to foreground) // This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e) { MessageBox.Show("Activated"); PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; }
// Code to execute when the application is deactivated (sent to background) // This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e) { MessageBox.Show("Deactivated"); PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled; }
// Code to execute when the application is closing (eg, user hit Back) // This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e) { MessageBox.Show("Closing"); PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled; }
This ends the article enabling and disabling of autolock screen in Windows Phone 7.
|