gpt4 book ai didi

windows - 后台 Windows Phone 8.1 (WinRT) 中的地理围栏

转载 作者:可可西里 更新时间:2023-11-01 13:26:51 24 4
gpt4 key购买 nike

问题

我正在尝试在 WP8.1 (WinRT) 中发生地理围栏事件(进入/退出)时触发 BackgroundTask。我已经编写了一个示例应用程序来尝试让它工作,但似乎无法做到这一点。

到目前为止,这些是我尝试让 Geofences 在后台运行所采取的步骤:

  1. 检查定位功能
  2. 创建+注册地理围栏
  3. 创建并注册一个监听 LocationTrigger(LocationTriggerType.Geofence);
  4. 的 BackgroundTask
  5. 在我的后台任务中,触发一个简单的弹出通知

我为解决问题所做的事情

我在我的 app.manifest 中启用了:

  • Toast Capable => 是
  • 能力:定位、互联网(客户和服务器)
  • 声明:BackgroundTasks(位置)。 EntryPoint = BackgroundTask.GeofenceBackgroundTask

我的后台任务位于一个名为 BackgroundTask 的单独项目中。它是一个 WindowsRT 组件,包含一个类 GeofenceBackgroundTask

Project Layout

示例项目

可以在这个[链接]( https://github.com/kiangtengl/GeofenceSample ) 找到该项目的代码:

如何测试

  1. 在模拟器中运行代码

  2. 将位置设置为:纬度 = 01.3369,经度 = 103.7364

enter image description here

  1. 点击注册地理围栏 + BackgroundTasks 按钮

  2. 退出应用(按主页按钮)

  3. 将当前位置更改为距您先前设置的位置 100 米的任何位置。应该会弹出一条通知。

项目代码:

检查定位功能

    public static async Task GetLocationCapabilities()
{
try
{
var geolocator = new Geolocator();
await geolocator.GetGeopositionAsync();
var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
Debug.WriteLine("background access status" + backgroundAccessStatus);
}
catch (UnauthorizedAccessException e)
{
Debug.WriteLine(e);
}
catch (TaskCanceledException e)
{
Debug.WriteLine(e);
}
}

创建地理围栏

    public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default")
{
// The Geofence is a circular area centered at (latitude, longitude) point, with the
// radius in meter.
var geocircle = new Geocircle(position, radius);

// Sets the events that we want to handle: in this case, the entrace and the exit
// from an area of intereset.
var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;

// Specifies for how much time the user must have entered/exited the area before
// receiving the notification.
var dwellTime = TimeSpan.FromSeconds(1);

// Creates the Geofence and adds it to the GeofenceMonitor.
var geofence = new Geofence(id, geocircle, mask, false, dwellTime);

try
{
GeofenceMonitor.Current.Geofences.Add(geofence);
}
catch (Exception e)
{
Debug.WriteLine(e);
// geofence already added to system
}

}

注册后台任务

    public static async Task RegisterBackgroundTask()
{
try
{
// Create a new background task builder
var geofenceTaskBuilder = new BackgroundTaskBuilder()
{
Name = GeofenceBackgroundTaskName,
TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask"
};

// Create a new location trigger
var trigger = new LocationTrigger(LocationTriggerType.Geofence);

// Associate the location trigger with the background task builder
geofenceTaskBuilder.SetTrigger(trigger);

var geofenceTask = geofenceTaskBuilder.Register();

// Associate an event handler with the new background task
geofenceTask.Completed += (sender, e) =>
{
try
{
e.CheckResult();
}
catch(Exception error)
{
Debug.WriteLine(error);
}
};
}
catch(Exception e)
{
// Background task probably exists

Debug.WriteLine(e);
}
}

触发Toast的BackgroundTask代码

namespace BackgroundTask
{
public sealed class GeofenceBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var toastTemplate = ToastTemplateType.ToastText02;
var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate);
var textElements = toastXML.GetElementsByTagName("text");
textElements[0].AppendChild(toastXML.CreateTextNode("You have left!"));

var toast = new ToastNotification(toastXML);

ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}
}

最佳答案

我发现上面的代码示例以及上面的代码都有效。我遇到的问题是 Windows Phone 8.1 不会自动触发地理围栏事件。在触发 BackgroundTask 之前,您必须等待一定时间 <5 分钟。

这也适用于前景中的地理围栏。

关于windows - 后台 Windows Phone 8.1 (WinRT) 中的地理围栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23608073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com