WP7 – Runing App under the Locked Screen详述

Detailed description of Windows Phone 7 – Runing App under the Locked Screen

Shared in the old dog This article is about how to make the developed App can continue to execute under

Locked Screen. It makes me find it very interesting and also brings a lot of ideas into it, so I want to make it a more detailed explanation,

Help me to learn more quickly in reading.

First emphasize a concept, the so-called support for running under the Locked Screen:

“Currently the App is executing, the task of pressing the Locked Screen; or After the App has been executed for a period of time, the phone automatically enters the Locked Screen task.”

This is the part that needs to be clarified first. The supported function is not the case where the App can still work while pressing the Windows key or the Back key. .

[Note]

The content of this article is actually not obvious through the test of WP7 Emulator. It is recommended that if you have a WP7 device, The test will feel better.

In addition, maybe you can modify the WP7 Emulator file to get the time to set the screen off, maybe this will have a good test effect.

Before explaining that WP7 supports App running under the Locked Screen, I still have to mention again about the original WP7 definition of App running life cycle:

>The life cycle of the basic WP7 App

In the article , there are Mention the LifeCycle of the entire WP7 App, you can refer to it.

In the entire LifeCycle, the key point is: WP7 App will be executed as the user clicks the Back key, Start key, enters the Locked Screen or involves Start

Other Lanucher, Chooser, Closing or Tombstoning the current App. The difference between the two processing will affect the content of the program and the user’s operating habits.

〉The life cycle of WP7 App running under Locked Screen

According to the life cycle of the above WP7 App, it is known that when entering the Locked Screen mode, the App will automatically enter tombstoning, but the App will be temporarily stopped.

So to support the App running under Locked Screen, the main important point is: it no longer performs tombstoning. In other words, it skipped the Deactived? Event process.

However, let the App not enter the Deactived Event processing, which means that the App still uses the same memory and resources under the Locked Screen as under the Unlocked Screen,

But in fact, this is unnecessary Yes, therefore, in addition to allowing the App to work after entering the Locked Screen, another important point is to effectively save the usage of resources.

?

According to The mentioned key technologies can be run under Locked Screen:

(1) Set PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled< /p>

(2) Handling two important events in the App’s RootFrame: Obscured and Unobscured, which handle the tasks of Locked and Unlocked respectively

The following will be explained separately:< /p>

-Key attribute: “PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled”

?PhoneApplicationService (Microsoft.Phone.Shell )

This type of task manages various aspects of the entire application life cycle, including: managing the behavior of the application when it is idle, and managing the state of the application

Reason (for example: Launching, Closing, Activated, Deactived of the application)… etc., and it controls the properties and events of the entire App running. The use of this type can be seen frequently in “App.xaml.cs” in the project. PhoneApplicatoinService has several very important attributes:

Name Description
ApplicationIdleDetectionMode Set/get whether application idle detection is activated. Enabled this attribute, when the App enters Idle mode, the operating system will deactive idle applicatoin. Disabled this attribute means that the system will not perform what Enabled should do, which will affect battery usage and performance.

使用该属性要特别注意,因为即便进入Locked Screen下,App仍会消耗设备的电池。
For more details, please refer to Idle Detection for Windows Phone.

使用的值是:IdleDetectionMode枚举。

Current (*) Get PhoneApplicationService Object and current App instance.

通常该PhoneApplicatoinService对象与App实例都是由WP7 OS自动产生的,所以控制的机会比较少,但关于Locked Screen下的运行,如何控制目前的PhoneApplicatoinService对象与实例就很重要。

更详细可参考Execution Model Best Practices for Windows Phone。

StartupMode Get the current App startup mode. Since the mode of App startup may come from starting after the Start button or executing mutual calls between apps, etc., XNA-based apps are usually used because they need to monitor the program to enter before and after the Activated or Launching event is triggered. Conversion of joint capacity or action.
State Dictioncary object, used to store the App When entering the Deactived event, temporarily store the information and content of the current App, and wait until it is transferred back to the Activated evetn.
UserIdleDetectionMode Set/Getuser Whether idle detection is activated. Enabled this attribute, when the user is idle for a period of time, the system will automatically reduce the resources and operating performance of the App; on the contrary, the Disabled attribute will not perform the above tasks.

然而会使用Disabled的值,通常是在设计游戏上会使用,因为用户可能停止一段时间不操作手机(例如:等待下载、loading、动画等)。 For more details, please refer to Idle Detection for Windows Phone.

使用的值是:IdleDetectionMode枚举。

From the description of the above four attributes, it is not difficult to see what attributes should be used by the App that will continue to run under the Locked Screen to be practiced this time. Bar. Nothing wrong, it is the most complicated

“Current” property. Since the Current property itself represents an object behind an instance of PhoneApplicatoinService, it also has the above four important properties.

In order to support the App can still run under Locked Screen, you must set: “ApplicationIdleDetectionMode=IdleDetectionMode.Disabled”,

After setting to Disabled, WP7 OS itself will not automatically trigger the Deactived Event and turn the App into a Tombstone when the App enters Idle. In this way, the program can continue

execution, but how the program ends and the actions that require processing resources and performance become the responsibility of the developer.

But you must first pay attention to one thing, “When ApplicationIdleDetectionMode is set to Disabled, there is no way to switch the current execution back to Enabled, and it needs to be restarted

The App can only be set (that is, after PhoneApplicationFrame is initialized), otherwise an InvalidOperationException exception will occur.”

In addition, introduce two used attributes and classes:

?IdleDetectionMode

Member name Description
Enabled UserIdleDetectionMode: When Touch Event for a while If there is no trigger, the system will consider transferring the App to Idle.
ApplicationIdleDetectionMode: When the Lock screen is triggered, the system will automatically suspend App activity.
Disabled UserIdleDetectionMode: When there is no Touch Event for a while When triggered, the system will not consider transferring the App to Idle.
ApplicationIdleDetectionMode: When the Lock screen is triggered, the executing App will continue to execute.

?PhoneApplicationFrame (Microsoft.Phone.Controls)

Because WP7 is mainly in Silverlight page mode Running, users can browse the content in different Pages through NavigationService. However, the main core content of the page mode is

A Top-level container control: PhoneApplicatoinFrame to manage the processing of Page And browsing, in addition, it can also control the PhoneApplicationPage control. More detailed related pages

For the operation of page mode, please refer to: Frame and Page Navigation Overview for Windows Phone.

?

PhoneApplicationFrame manages all pages of the current App. Therefore, when the Locked Screen is activated, the content and events in the Page are actually affected by the App. It will be unavailable.

It is similar to adding a layer of film (Locked program) to the App to cover the Page on the original App, but the PhoneApplicatoinFrame on the App has a support event that can handle the Locked Screen

The trigger event brought by. Below, I will introduce two important Event Handlers, Handle the trigger event when the App enters Idle (Obscured) and trigger the event when Idle enters the App (Unobscured).

?

-Monitoring events: “Obscured” and “Unobscured”

? Obscured / ObscuredEventArgs (EventHandler )

This event is triggered when Sell Chrome is overlaid on the Frame. For example: Triggered when the application receives the Locked Screen event.

Support execution under Locked Screen, which means that the App itself has not stopped, but a Shell Chrome is covered on top. At this time, you cannot operate the functions on the App screen.

Therefore, it is recommended to stop some related UI special effects, animations, network connections (if it is an exception to the Download function) and other functions to reduce the battery usage of the mobile phone.

?Unobscured / EventArgs

When Shell Chrome no longer covers the Frame, this event is triggered. For example: when the Locked Screen returns to the application, it is triggered.

When you return to the App from the Locked Screen, the Shell Chrome covered on it will disappear, and the UI and functions of the App that were stopped when the Locked Screen was stopped will be restored.

?

——————-

The above mentioned The PhoneApplicationService, PhoneApplicatoinFrame classes and Obscured and Unobscured event processing are all the main elements that must be completed so that the App can support the screen lock and continue to execute.

?

Next, I will briefly introduce the relevant practice procedures:

? Set PhoneApplicationService.Current. ApplicationIdleDetectionMode and the processing of registered Obscured and Unobscured events.

1: if (runsUnderLock) 

2: {

 3: //Set the current App's IdleDetectionMode=Disabled, so that WP7 OS will not convert the program to tombstone

4: PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled; 

5:? 

6: //Get RootVisual, used to register Obscured and Uobscured event processing, customer Control the tasks to be completed by the App under Locked and Unlocked

7: PhoneApplicationFrame rootframe = App.Current.RootVisual as PhoneApplicationFrame ;

9: System.Diagnostics.Debug.Assert(rootframe != null, "This sample assumes RootVisual has been set");   
10: if (rootframe != null)

11: {

13: rootframe.Unobscured += new EventHandler(rootframe_Unobscured);

14: }

15: 

16:} 

17: else 

18: { 

19: IsRestartRequired = true;

20: // we can not set it; we have to restart app ...

21: // PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Enabled ;

22: // This paragraph explains that because IdleDetectionMode cannot be changed during App execution, an EventHandler will be started to request the program to be restarted. 

23: EventHandler eh = RestartRequired;

24: if (eh != null)

25: eh(this, new EventArgs()); 

26: }

上述程序是撷取出 In one article: Part of the program in the ApplicationIdleModeHelper.cs class,

The purpose is:

(1) With the setting of the RunsUnderLock attribute value, identify the runsUnderLock variable Do App need to be set to IdleDetectionMode.Disabled;

???? If yes, set; otherwise, notify to restart the program;

(2) After setting, The set value will be saved in IsolatedStorage.

(3) When the App is started next time, take out the Setting saved in IsolatedStorage, identify it, and reset the RunsUnderLock attribute.

?

Finally, for more detailed sample content, please refer to The example description provided in the article,

The example uses Pivot to show two different execution methods:

?Under "greedy pivot"

Description that after PhoneApplicationService.Current.ApplicationIdleDetectionMode is set to Enabled and Disabled, the App can play music normally under Locked;

?

?Under "mellow pivot"

Through the practice of Obscured and Unobscured events, it shows that even if you support "Run? under lock", you can enter Locked After that, the music is still turned off, but when you Unlocked, the music will automatically play again. Emphasizes how to practice temporarily stop the App function under Locked (not tombstone"), the purpose is to save battery and performance consumption.

?

?

[Supplement]

When I read the program, I encountered Some missing concepts, so here are some additional parts that should be noted and thought about in development:

INotifyPropertyChanged< /strong>

INotifyPropertyChanged belongs to: System.ComponentModel, this class often appears in for customized components that need to practice the property changes behind it to trigger notification actions

< p>Important interface that will definitely be practiced. If you have referenced the this book, several examples in it also use this interface to practice extracting specific properties< /p>

Processing of sexual variables. What are the benefits of practicing this interface?

?Linked effect: INotifyPropertyChanged is most commonly used in Data Bind to process the data of a control And the situation that affects the next control.

?Automatic response: Objects or classes that support this type, as long as the interface is implemented in the data processing, it will automatically reflect the corresponding Control properties.

?

Since INotifyPropertyChanged handles the Data Bind to change the data, the App knows how to change the content on the UI, then Which things should be practiced?

?PropertyChangedEventHandler

This Event Handler is the necessary event processing for INotifyPropertyChanged. Its task is usually used to notify a certain specified property to be modified.

When the control receives a notification, it will process Bind Data to respond to the result of property adjustment.

For practical examples, please refer to: Bind Better with INotifyPropertyChanged.< /p>

? More detailed INotifyProper The content of tyChanged

I recommend This article is also suitable for reading. It mentions the definition and application of ViewModel.

And further explain the association between INotifyPropertyChanged and DependencyObjects.

?

-PhoneApplicationFrame and PhoneApplicationPage

http://msdn.microsoft.com/en-us/library/ff402536(v=VS.92).aspx

In a PhoneApplicationFrame, it includes the PhoneApplicationPage control and other rich other controls under Microsoft.Phone.Controls.

However, the PhoneApplicationPage control is responsible for managing the current Content Area in the Frame. All the tasks and content in it.

The picture above is the best way to explain the relationship between the entire Frame and Page.

?

References:

?Running a Windows Phone Application under the lock screen

p>

?wpf data binding, INotifyPropertyChanged vs DependencyProperty (—)

?INotifyPropertyChanged and WPF

?How to: Implement the INotifyPropertyChanged Interface

?PhoneApplicationPage Control for Windows Phone

?PhoneApplicationFrame Control for Windows Phone

? "Programming WPF" Translation Chapter 4 2. Data Binding (2)

?Base Controls for Windows Phone (Supported & Unsupported)

?Class Library Reference for Windows Phone

?

Dotblogs tag: PhoneApplicationService, PhoneApplicationFrame ,PhoneApplicationPage

Original: Big Column WP7-Runing App under Details of the Locked Screen

Leave a Comment

Your email address will not be published.