我正在开发一个需要定期检测用户最后位置的应用程序。我使用了一些我在谷歌上找到的教程,但它们都不起作用,需要帮助。这是我的代码中的一个示例。
我在 app.xaml.cs 中创建了我的 schdeuledactionservice。我已经在 WMAppManifest.xml 中添加了我的扩展任务。
来自 WMAppManifest.xml
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="ToDoSchedulerAgent" Source="ToDoSchedulerAgent" Type="ToDoSchedulerAgent.TaskScheduler" />
</ExtendedTask>
调度代理代码:
public class TaskScheduler : ScheduledTaskAgent
{
public const string Name = "ToDoLocator";
public static Location location;
protected override void OnInvoke(ScheduledTask task)
{
if (task is PeriodicTask)
{
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 10
};
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(3));
watcher.Start();
}
NotifyComplete();
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
throw new NotImplementedException();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => PositionChanged(e));
}
void PositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
location = new Location();
location.Latitude = e.Position.Location.Latitude;
location.Longitude = e.Position.Location.Longitude;
}
}