背景中的地理围栏Windows Phone 8.1(WinRT) 问题
我试图在WP8.1(WinRT)中发生地理围栏事件(进入/退出)时触发BackgroundTask。我已经编写了一个示例应用程序,试图让它工作,但似乎无法做到这一点 到目前为止,我已经采取了以下步骤,试图让GeoFence在后台工作:背景中的地理围栏Windows Phone 8.1(WinRT) 问题,windows,windows-phone-8,geolocation,windows-runtime,windows-phone-8.1,Windows,Windows Phone 8,Geolocation,Windows Runtime,Windows Phone 8.1,我试图在WP8.1(WinRT)中发生地理围栏事件(进入/退出)时触发BackgroundTask。我已经编写了一个示例应用程序,试图让它工作,但似乎无法做到这一点 到目前为止,我已经采取了以下步骤,试图让GeoFence在后台工作: 检查位置功能 创建并注册地理围栏 创建并注册侦听LocationTrigger(LocationTriggerType.geofiness)的BackgroundTask 在我的后台任务中,触发一个简单的弹出通知 我为排除故障所做的事情 我已在我的app.mani
LocationTrigger(LocationTriggerType.geofiness)的BackgroundTask代码>
- Toast Capable=>是
- 功能:位置、互联网(客户端)& 服务器)
- 声明:背景任务(位置)。入口点=
BackgroundTask.GeofenceBackgroundTask
BackgroundTask
。它是一个WindowsRT组件,包含一个类GeofenceBackgroundTask
样本项目
该项目的代码可在此[链接]()找到:
如何测试
我发现上面的代码示例和上面的代码都是有效的。我面临的问题是Windows Phone 8.1不会自动触发地理围栏事件。你必须等待一定的时间我正忙着做同样的事情,我也注意到了这种行为,但对我来说是2分钟。
不幸的是,它总是在2分钟后触发,即使位置没有变化,仍然在围栏内。请再次上传示例项目好吗?不可用:(
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);
}
}
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);
}
}
}