我正在工作的一个项目支持位置跟踪,最近我们决定在应用程序处于后台时添加对位置跟踪的支持。我们支持以下平台:iOS,Android和UWP。顾名思义,我正在寻求帮助在UWP(台式机和电话)上实现此功能。我想知道是否有人可以帮我或者为一些为UWP编写GPS /运行跟踪应用程序的人指出一些代码。
我已经尝试了扩展执行会话和后台任务。实际上,我目前正在我的应用程序中同时使用这两种软件,而且都没有运气让后台跟踪在UWP上运行。我们正在使用的位置服务是基于MvvmCross UWP Location Watcher的自定义代码。作为参考,摘自项目App.xaml.cs
的一些摘录:
// This method is associated with the LeavingBackground event
private async void WillEnterForeground(object sender, LeavingBackgroundEventArgs e)
{
if (_trackingNotificationTask == null)
{
await BackgroundExecutionManager.RequestAccessAsync();
RegisterTrackingNotificationTask();
}
if (_backgroundTrackingSession == null)
{
using (var backgroundSession = new ExtendedExecutionSession())
{
backgroundSession.Reason = ExtendedExecutionReason.LocationTracking;
backgroundSession.Description = "Track Responder Location in Background";
backgroundSession.Revoked += OnSessionRevoked;
var result = await backgroundSession.RequestExtensionAsync();
switch (result)
{
case ExtendedExecutionResult.Allowed:
_backgroundTrackingSession = backgroundSession;
break;
}
}
}
var locationService = Mvx.Resolve<ILocationService>();
locationService?.Resume();
if (_appInBackground)
{
ClearAllNotifications();
if (_backgroundTrackingTask != null)
{
_backgroundTrackingTask.Unregister(true);
_backgroundTrackingTask = null;
}
await Mvx.Resolve<IEventInformationSyncService>().SyncUserSetting();
await Mvx.Resolve<IAppService>().IsMobileStatusGood();
_appInBackground = false;
}
}
// This Method is associated with the EnteredBackground event
private void DidEnterBackground(object sender, EnteredBackgroundEventArgs e)
{
var deferral = e.GetDeferral();
// Request permission for background tracking
RegisterBackgroundTrackingTask();
var appSettingService = Mvx.Resolve<IAppSettingService>();
if (!NeedToUpgradeWindows() && Core.App.IsResponderTrackingEnabled()) // Are we in an event and is responder tracking enabled?
{
ShowNotification();
}
_appInBackground = true;
deferral.Complete();
}
private void RegisterBackgroundTrackingTask()
{
if (NeedToUpgradeWindows())
{
if (!_shownUpgradeMessage)
{
Mvx.Resolve<IUserInteraction>().Alert("Your current windows version does not support background tasks. Please upgrade to windows version 1709 or above to utilize background tasks.", () => { _shownUpgradeMessage = true; });
}
return;
}
var backgroundTrackingBuilder = new BackgroundTaskBuilder()
{
Name = BackgroundTrackingTask.Name,
TaskEntryPoint = typeof(BackgroundTrackingTask).FullName,
IsNetworkRequested = true
};
backgroundTrackingBuilder.SetTrigger(new TimeTrigger(15, false));
_backgroundTrackingTask = backgroundTrackingBuilder.Register();
_backgroundTrackingTask.Completed += OnBackgroundTrackingTaskCompleted;
}
private void RegisterTrackingNotificationTask()
{
if (NeedToUpgradeWindows())
{
if (!_shownUpgradeMessage)
{
Mvx.Resolve<IUserInteraction>().Alert("Your current windows version does not support background tasks. Please upgrade to windows version 1709 or above to utilize background tasks.", () => { _shownUpgradeMessage = true; });
}
return;
}
if (IsTaskRegistered(TrackingNotificationHandlerTask.Name))
{
return;
}
var notificationTaskBuilder = new BackgroundTaskBuilder()
{
Name = TrackingNotificationHandlerTask.Name,
TaskEntryPoint = typeof(TrackingNotificationHandlerTask).FullName
};
notificationTaskBuilder.SetTrigger(new ToastNotificationActionTrigger());
_trackingNotificationTask = notificationTaskBuilder.Register();
_trackingNotificationTask.Completed += OnNotificationActivated;
}
private void OnBackgroundTrackingTaskCompleted(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
var settingValues = ApplicationData.Current.LocalSettings.Values;
var success = (bool)settingValues["Success"];
if (success)
{
var locationService = Mvx.Resolve<ILocationService>();
var location = new MvxGeoLocation
{
Coordinates = new MvxCoordinates
{
Latitude = (double)settingValues["Latitude"],
Longitude = (double)settingValues["Longitude"],
Accuracy = (double)settingValues["Accuracy"]
},
Timestamp = new DateTimeOffset(DateTime.Parse((string)settingValues["Time"]))
};
locationService.OnLocation(location, true);
}
}
private void OnNotificationActivated(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
var settings = ApplicationData.Current.LocalSettings;
var action = (string)settings.Values["NotificationAction"];
var locationService = Mvx.Resolve<ILocationService>();
switch (action)
{
case "pause":
locationService?.ClearResponderTrackingOptions();
break;
case "resume":
locationService?.RecycleResponderTracking();
break;
}
ClearAllNotifications();
ShowNotification();
}
非常感谢大家。请让我知道你们是否还需要其他东西!