Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 7 Execution Model : Managing Obscured and Unobscured Events

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server