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
- Communicating with Internet Email : Sending Messages (part 1) - Taking Control of Your Messages, Creating a Signature, Creating an Email Shortcut for a Recipient
- Microsoft Dynamic AX 2009 : Working with .NET Business Connector (part 1) - Processing Requests and Responses
- Microsoft Visio 2010 : Creating and Validating Process Diagrams - Creating Subprocesses
- Automating Windows 7 Installation : Customizing Images Using Deployment Image Servicing and Management (part 2) - Mounting an Image , Servicing Drivers in an Image
- Microsoft Systems Management Server 2003 : Configuring the Client (part 2) - Running Advertised Programs on Clients - Advertised Programs Wizard
- Securing Your SharePoint and Windows Azure Solutions : Create a Windows Forms Application to Display the Shared Access Permissions Signature
- Adobe Flash Catalyst CS5 : Applying and removing filters
- Windows Server 2012 : Configuring IPv6/IPv4 interoperability (part 2) - Default IPv6 functionality
- Extending the Real-Time Communications Functionality of Exchange Server 2007 : Exploring Office Communications Server Tools and Concepts
- SharePoint 2010 : Farm Governance - Installing a feature and activating it
Top 10
- Windows Phone 8 : Scheduled Tasks - Scheduled Task API Limitations
- Windows Phone 8 : Scheduled Tasks - Updating Tiles Using a Scheduled Task Agent
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 5) - Editing an Existing To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 4) - Creating the To-Do Item Shell Tile, Saving a To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 3) - Debugging Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 2) - TodoService, TodoItemViewModel
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 1) - TodoItem,TodoDataContext
- Windows Phone 8 : Scheduled Tasks - Using Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - Background Agent Types
- Windows Phone 8 : Windows Phone Toolkit Animated Page Transitions - Reusing the Transition Attached Properties
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro