UWP 位置跟踪,即使应用程序已暂停

UWP location tracking even when the app was suspended

我正在开发一个 UWP 运行 应用程序,我想记录用户的真实位置 time.I已经阅读了很多帖子 google,并得到了一些解决方案: ExtendedExecutionSession , app service。但是这两种方案都有一个共同的问题:应用挂起时无法记录位置。我知道 uwp 中的后台任务,似乎需要一个触发器(例如 SystemTrigger)才能启动。当应用程序暂停到 track/record 位置时,是否可以启动(不使用触发器而是直接调用)自定义后台任务?

首先我想回答你的问题:

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

是的,您可以使用 ApplicationTrigger class 执行此操作。参见 https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.applicationtrigger (它仍然是一个触发器,因为 API 需要它,但它基本上直接从应用程序调用后台任务)

但这里有一些用于执行位置跟踪和此类操作的示例: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Geolocation

我认为您正在寻找的是 LocationTrigger(参见:https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.locationtrigger) 它用于地理围栏示例 (Scenario5_GeofenceBackgroundTask.xaml.cs)

基本上是这样注册的:(来自示例代码)

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...)