I am trying to use Extended Execution on my Windows 10 UWP app for the purpose of location tracking. There are many examples of this online. I am basically using this common block of code.
using (var session = new ExtendedExecutionSession())
{
session.Reason = ExtendedExecutionReason.LocationTracking;
session.Description = "Tracking your location";
session.Revoked += NewSession_Revoked;
var result = await session.RequestExtensionAsync();
switch (result)
{
case ExtendedExecutionResult.Allowed:
await StartLocationTrackingAsync();
break;
default:
case ExtendedExecutionResult.Denied:
//Notify user or log this.
break;
}
}
This block of code is executed in the Suspending event handler for my app, like this:
public Scenario1()
{
this.InitializeComponent();
Application.Current.Suspending += Current_Suspending;
}
private async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
//Do my extended execution request here.
}
The first issue I had was I could not get that event to even fire but I read on SO that VS will not suspend your app while debugging. So, I forced it to suspend using the Lifecycle events drop down in VS. Now, I get the Suspending event to raise each time, which is good.
However, when I request the extended execution session, it is denied every single time. I have tried on the emulator and on my physical W10 Mobile device, multiple times each and every time, it is denied.
Why is this? How can I get it to be allowed?
Thanks!
Assuming you have already declared Location in the app manifest, you will have to run the code in the foreground, which means it will work in your MainPage_Loaded
callback but not in the suspended state.
A location tracking extended execution session can run as long as needed. However, there can only be one such session running per device. A location tracking extended execution session can only be requested in the foreground, and the app must be in the Running state. This ensures that the user is aware that the app has initiated an extended location tracking session.
To run this in the background, see this -
It is still possible to use the GeoLocator while the app is in the background by using a background task, or an app service, without requesting a location tracking extended execution session.
You use LocationTracking
extended execution when you -
Specify ExtendedExecutionReason.LocationTracking when you create an ExtendedExecutionSession if your app needs to regularly log the location from the GeoLocator. Apps for fitness tracking and navigation that need to regularly monitor the user's location and should use this reason.
Here is the full article on this.