1. Problem
You need to create an
application that continues to run either when the Windows Phone
operating system locks the phone screen or you receive a phone call.
During those events, you have to power off some battery-consuming
resources. Then, when the screen is unlocked or the phone call ends, you
have to turn those resources on again.
2. Solution
You have to use the ApplicationIdleDetectionMode property provided by the PhoneApplicationService class and register both Obscured and Unobscured event handlers defined in the PhoneApplicationFrame class.
3. How It Works
ApplicationIdleDetectionMode is a property that is defined in the PhoneApplicationService class and that has its default value set to IdleDetectionMode.Enabled. This means that the application is suspended when the operating system finds it idle. You can set the property to IdleDetectionMode.Disabled so that your application will continue to run even when the operating system locks the screen.
NOTE
The lock screen idle time is defined in the Settings of your physical phone and is not configurable from the emulator.
The Obscured event is raised when the phone screen is locked by the Windows Phone 7 operating system or when you receive a phone call. The Unobscured event is raised when the phone screen is unlocked or when the phone call is terminated.
When the IdleDetectionMode property of ApplicationIdleDetectionMode is set to Disabled,
you can use those two methods to stop power-consuming resources (such
as the accelerometer, screen animations, FM radio service, and so on)
and then start them again when the lock screen is removed. In the Obscured
event handler, you should specify the code to stop useless the service
is one that your application is not going to use because the phone
screen is locked. So if your application uses the StoryBoard to show an
animated progress bar, you will stop the animation but upgrade the
progress bar logic. You should stop other services to reduce battery
consumption.
When IdleDetectionMode is set to Enabled, you can use Obscured and Unobscured
event handlers to manage particular events that are not raised with
tombstoning. Indeed, the phone lock screen and phone calls don't raise
tombstoning events. So, for example, if you have created a game with
Silverlight and you want to show a pause screen after a phone call ends,
you have to use the Obscured and Unobscured events.
NOTE
The OnDeactivated event handler from the XNA Game class receives the phone screen lock and the phone call events. So it is not necessary to implement the Obscured and Unobscured event handlers to stop a game. The OnDeactivated event handler remains necessary when you disable idle detection mode.
4. The Code
Open Visual Studio 2010 and create a new Windows Phone 7 Silverlight application called ObscuredUnobscuredApp. In MainPage.xaml.cs, we added the code to the MainPage constructor in order to register the Obscured and Unobscured event handlers and to set the IdleDetectionMode.Disabled value to ApplicationIdleDetectionMode.
public MainPage()
{
InitializeComponent();
PhoneApplicationService.Current.ApplicationIdleDetectionMode =
IdleDetectionMode.Disabled;
PhoneApplicationFrame rootFrame = ((App)Application.Current).RootFrame;
rootFrame.Obscured += new EventHandler<ObscuredEventArgs>(rootFrame_Obscured);
rootFrame.Unobscured += new EventHandler(rootFrame_Unobscured);
}
NOTE
A restart application is required if you change the IdleDetectionMode
value twice at runtime. The official documentation indicates that this
behavior could change in future operating system releases.
The Obscured event handler provides the ObscuredEventArgs parameter, which defines the IsLocked property used to know if the event is raised either by the phone screen lock (its value is true) or by the phone call (its value is false).
In the Obscured and Unobscured event handlers, we are going to disable and then enable the battery's power-consuming services, respectively.
void rootFrame_Unobscured(object sender, EventArgs e)
{
FMRadio.Instance.PowerMode = RadioPowerMode.On;
acc.Start();
geoW.Start();
}
void rootFrame_Obscured(object sender, ObscuredEventArgs e)
{
FMRadio.Instance.PowerMode = RadioPowerMode.Off;
acc.Stop();
geoW.Stop();
}
5. Usage
There is no way to test the application by using the Obscured and Unobscured
event handlers in the emulator. So press F5 only if you changed the
output target to the Windows Phone 7 device. You have to put two
breakpoints in the Obscured and Unobscured code so you can see that events are raised.
The application will start,
briefly showing the main page. Now you can call your phone with another
phone or wait while the Windows Phone operating system locks the
screen. Note the application hitting breakpoints.