Floating FB popout byF5debug

101 How to on Windows Phone – How to #39 – Programmatically controls Screen Auto Locking in Windows Phone

 

In this tutorial we are going to see how to control the AutoLocking of Windows Phone Device when an application is running, or we want to disable it permanently when an application is running. This feature is a much for security where it auto locks if its idle for quite sometime, but there are some applications where the auto locking feature should be disabled on using that application. We can do that programmatically by using the PhoneApplicationServices which is available by default with the APP.XAML page which is a mandatory page loaded with the project.       [more]

Let us see the steps on how to achieve this task on controlling the AutoLocking in Windows Phone. To start with Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below.

clip_image002

Now design the page as its not in our scope we can take it granted that our application is designed and developed with the necessary controls and process and its working fine. Now go the APP.XAML Page and we can see the ApplicationLaunching Service with the list of available events as shown in the screen below.

clip_image004

Now let us go to the code behind App.Xaml.Cs and we can see a separate events available for each of the events on Launching, Closing, Activated and Deactivated as shown in the screen below. The name on these describes the need of these events, so lets not waste time in telling what exactly it means.

clip_image006

In each of the events we can write a small message to indicate which event is trigger at what time by using a message box. Now in Windows Phone we have a PhoneApplicationService class which is mainly used to maintain the application lifecycle with different options across the different phases of the cycle. It also helps to maintain the idle behavior of the application manage the applications state. PhoneApplicationService has many properties out of which we ApplicationIdleDetectionMode which is used to identify the behavior of the application. It has an enum which takes the values of Enable or Disable and as per MSDN, When application idle detection is enabled, the operating system will deactivate idle applications. Disabling application idle detection requests that the operating system not perform this service. This feature should be used with caution because applications that run while the phone is locked continue to drain the device’s battery. In this case, the user may not realize that the application is running because the screen is off and the phone looks idle. So we have added the ApplicationIdle behavior to enabled and disabled as per the event trigger as shown in the code below.

Code:

// Code to execute when the application is launching (e.g., from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
    MessageBox.Show("Application 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("Application 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("Application 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("Application Closing");
    PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
}

clip_image008

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

Output Screen:

clip_image010

That’s it from this tutorial, hope this was quite interesting and can be used with the Application development. See you all in the next tutorial will an interesting topic on How to with Windows Phone until then Happy Programming.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter