I‘m developing a UWP running app, and I want to record user's location in real time.I've read many threads throw google, and got some solutions as: ExtendedExecutionSession , app service. But these two solutions have a same problem: cannot record the location when app is suspended. I know the background task in uwp, it seems need a trigger(e. g. SystemTrigger) to launch. Is it possible to launch (do not use trigger but call directly) a custom background task when app is suspended to track/record location?

1 Answers

1
gregkalapos On Best Solutions

First I would like to answer your question:

Is it possible to launch (do not use trigger but call directly) a custom background task when app is suspended to track/record location?

Yes, you can do this with the ApplicationTrigger class. See https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.applicationtrigger (It's still a trigger, since the API needs it, but it basically calls the backgroundtask directly from the app)

But here are some samples for doing location tracking and these kind of things: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Geolocation

I think what you are looking for is the LocationTrigger (see: https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.locationtrigger) It is used in the Geofence sample (Scenario5_GeofenceBackgroundTask.xaml.cs)

Basically you register it like this: (it’s from the sample code)

BackgroundTaskBuilder geofenceTaskBuilder = new BackgroundTaskBuilder();
geofenceTaskBuilder.Name = SampleBackgroundTaskName;
geofenceTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;

var trigger = new LocationTrigger(LocationTriggerType.Geofence);

geofenceTaskBuilder.SetTrigger(trigger);


_geofenceTask = geofenceTaskBuilder.Register();
//...and do the rest (but look into the sample..everything is from there...)