Logo
CAR REVIEW
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
PREGNANCY
 
 
Windows Phone

Windows Phone 7 Execution Model : Managing Obscured and Unobscured Events

7/10/2011 6:06:39 PM

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.

Other -----------------
- Windows Phone 7 Execution Model : Managing Tombstoning in Windows Phone 7 XNA Applications
- Windows Phone 7 Execution Model : Managing Tombstoning in Windows Phone 7 Silverlight Applications
- Windows Phone 7 Execution Model : Navigating Between Pages with State
- Windows Phone 7 Execution Model : Navigating Between Pages by Using Global Application Variables
- Windows Phone 7 Execution Model : Passing Data Through Pages
- Windows Phone 7 Execution Model : Navigating Between Pages
- Developing for Windows Phone 7 and Xbox 360 : Using the Content Pipeline - Content Importers
- Developing for Windows Phone 7 and Xbox 360 : Using the Content Pipeline - Content Processors
- Developing for Windows Phone 7 and Xbox 360 : Introduction to Custom Effects - Effect States
- Creating a Trial Windows Phone 7 Application
- Deploying the Windows Phone 7 Application on the Device
- Deploying the Application to the Windows Phone 7 Emulator
- Creating a Simple XNA Windows Phone 7 Application
- Creating a Simple Silverlight Windows Phone 7 Application
- Developing for Windows Phone 7 and Xbox 360 : Lighting (part 5) - Point Lights
- Developing for Windows Phone 7 and Xbox 360 : Lighting (part 4) - Fog
- Developing for Windows Phone 7 and Xbox 360 : Lighting (part 3) - Emissive Lighting & Specular Lighting
- Developing for Windows Phone 7 and Xbox 360 : Lighting (part 2) - Triangle Normals & Diffuse Lighting
- Developing for Windows Phone 7 and Xbox 360 : Lighting (part 1) - Ambient Lighting
- Developing for Windows Phone 7 and Xbox 360 : Introduction to Custom Effects - Vertex Color & Texturing
 
 
Most view of day
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 1) - OrientationChanged Event
- Integrating BizTalk Server 2010 and Microsoft Dynamics CRM : Communicating from BizTalk Server to Dynamics CRM (part 6)
- Managing Windows 7 : Creating a Linked Online ID, Managing Travel Settings
- Windows Server 2003 on HP ProLiant Servers : Server Placement (part 1) - DNS Placement, Site Affinity
- Designing and Configuring Unified Messaging in Exchange Server 2007 : Unified Messaging Shell Commands
- Customizing Windows 7 : Set the Screen Saver
- How to Troubleshoot USB Problems : How to Identify USB Problems Using Performance Monitor, How to Examine USB Hubs
- Microsoft Systems Management Server 2003 : Creating Packages for Distribution (part 1) - Creating a Package from Scratch
- Windows Phone 8 : Configuring Basic Device Settings - Date and Time (part 2) - Changing Time Zones, Using Military Time
- Administering an Exchange Server 2013 Environment (part 5) - Performing Common Tasks - Creating Multiple Mailboxes in the Exchange Management Shell
Top 10
- Microsoft Lync Server 2013 : Director Troubleshooting (part 3) - Synthetic Transactions,Telnet
- Microsoft Lync Server 2013 : Director Troubleshooting (part 2) - DNS Records, Logs
- Microsoft Lync Server 2013 : Director Troubleshooting (part 1) - Redirects, Certificates
- Microsoft Lync Server 2013 : Administration of the Director Role (part 4) - Services Management, Client Version Filter
- Microsoft Lync Server 2013 : Administration of the Director Role (part 3) - Topology Status
- Microsoft Lync Server 2013 : Administration of the Director Role (part 2) - Ports,Firewall Rules
- Microsoft Lync Server 2013 : Administration of the Director Role (part 1) - Services
- Microsoft Lync Server 2013 : Configuring the Director (part 2) - Web Services Ports,Reverse Proxy
- Microsoft Lync Server 2013 : Configuring the Director (part 1) - SRV Records, Web Services FQDN Overrides
- Sharepoint 2013 : SharePoint Designer 2013 (part 2) - Locking Down SharePoint Designer
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro