Win (Phone) 10 developer (7), Extended Execution, extendedexecution
As we all know, in WindowsPhone8, the app can continue to run when it is transferred to the background and has not been suspended. At this time, you can continue to execute program operations, this feature is useful in the location tracking app. It does not affect the program running after receiving a phone call or locking screen. However, in uap, this feature is unfortunately canceled.
In this case, ExtendedExecution is required for the app to continue running after the screen is locked, so that you can continue location tracking before the phone is suspended.
ExtendedExecution:
12345678910111213 |
Private async void StartLocationExtensionSession () {session = new ExtendedExecutionSession (); session. description = "Location Tracker"; session. reason = ExtendedExecutionReason. locationTracking; session. revoked + = ExtendedExecutionSession_Revoked; var result = await session. requestExtensionAsync (); if (result = ExtendedExecutionResult. denied) {MessageDialog md = new MessageDialog ("ExtendedExecution Denied. "); await md. showAsync ();}} |
ExtendedExecutionReason has three types: LocationTracking, SavingData, and Unspecified, which correspond to location tracking, data storage, and other operations respectively. If the first two methods are selected, but there is no corresponding method, the result of ExtendedExecutionResult. Denied will be obtained. For example:
12345678910111213141516171819202122232425 |
Private void MainPage_Loaded (object sender, RoutedEventArgs e) {DoWork (); StartLocationExtensionSession ();} private async void DoWork () {for (int I = 0; I <1000; I ++) {Debug. writeLine (I); await Task. delay (1000) ;}} private async void StartLocationExtensionSession () {session = new ExtendedExecutionSession (); session. description = "Location Tracker"; session. reason = ExtendedExecutionReason. locationTracking; var result = await session. requestExtensionAsync (); if (result = ExtendedExecutionResult. denied) {MessageDialog md = new MessageDialog ("ExtendedExecution Denied. "); await md. showAsync ();}} |
In fact, there is no LocationTracking operation, and this operation will be rejected.
In use, when RequestExtensionAsync selects the Reason correctly, there is no ExtendedExecutionSession_Revoked registration abolition event in case of Denied.
1234 |
Private void ExtendedExecutionSession_Revoked (object sender, ExtendedExecutionRevokedEventArgs args) {Debug. WriteLine ($ "Authorization: {ExtendedExecutionRevokedReason. SystemPolicy }");} |
There are two ExtendedExecutionRevokedReason instances, one SystemPolicy, which occurs when the front-end is restored after unlocking and another Resumed occurs when the resume is suspended.
However, after trying, the Revoked caused by SystemPolicy will continue to lock the screen and still run. After unlocking, the Revoked event will not be triggered.
When the location tracing or data saving ends, the session Dispose will be dropped, and repeated RequestExtensionAsync will report an error. In addition, in line with the principle of power saving, when the request is required, do not need to cancel.
12345 |
If (session! = Null) {session. Dispose (); session = null ;} |
In this way, you can lock the screen and continue to run the app.
During usage, we noticed another class: ExtendedExecutionForegroundSession. Its usage is almost the same as that of ExtendedExecutionSession. Its Reason has four types: BackgroundAudio, SavingData, Unconstrained, and Unspecified.
However, the results of RequestExtensionAsync are Denied. There is no documentation, so I don't know what to use. If you have any questions, please feel free to answer them.
More => http://www.liubaicai.net/