gpt4 book ai didi

c# - 后台任务地理围栏触发

转载 作者:行者123 更新时间:2023-12-03 05:07:48 25 4
gpt4 key购买 nike

当手机离开或进入地理围栏位置(在其他地方设置并传入)时,我试图显示Toast。问题是,当应用程序位于后台时,不会发生触发器,并且我看不到 showToast 消息。我正在使用电脑上的模拟器手动更改位置。

后台任务> 位置在应用 list 下设置。

这是我用来构建地理围栏和后台任务的代码

//Creates Geofence and names it "PetsnikkerVacationFence"
public static async Task SetupGeofence(double lat, double lon)
{
await RegisterBackgroundTasks();

if (IsTaskRegistered())
{

BasicGeoposition position = new BasicGeoposition();
position.Latitude = lat;
position.Longitude = lon;

double radius = 8046.72; //5 miles in meters
Geocircle geocircle = new Geocircle(position, radius);
MonitoredGeofenceStates monitoredStates = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;

Geofence geofence = new Geofence("PetsnikkerVacationFence", geocircle, monitoredStates, false);

GeofenceMonitor monitor = GeofenceMonitor.Current;

var existingFence = monitor.Geofences.SingleOrDefault(f => f.Id == "PetsnikkerVacationFence");

if (existingFence != null)
monitor.Geofences.Remove(existingFence);

monitor.Geofences.Add(geofence);

}




}

//Registers the background task with a LocationTrigger
static async Task RegisterBackgroundTasks()
{
var access = await BackgroundExecutionManager.RequestAccessAsync();


if (access == BackgroundAccessStatus.Denied)
{

}
else
{
var taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = "PetsnikkerVacationFence";

taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));

taskBuilder.TaskEntryPoint = typeof(Petsnikker.Windows.Background.GeofenceTask).FullName;

var registration = taskBuilder.Register();

registration.Completed += (sender, e) =>
{
try
{
e.CheckResult();
}
catch (Exception error)
{
Debug.WriteLine(error);
}
};



}

}

static bool IsTaskRegistered()
{
var Registered = false;
var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(keyval => keyval.Value.Name == "PetsnikkerVacationFence");
if (entry.Value != null)
Registered = true;
return Registered;
}
}
}

这段代码是我监视地理围栏状态的地方。这是 appxmanifest 中入口点所指向的位置

public sealed class GeofenceTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{

var monitor = GeofenceMonitor.Current;
if (monitor.Geofences.Any())
{
var reports = monitor.ReadReports();
foreach (var report in reports)
{


switch (report.NewState)
{
case GeofenceState.Entered:
{

ShowToast("Approaching Home",":-)");
break;
}
case GeofenceState.Exited:
{
ShowToast("Leaving Home", ":-)");
break;


}


}
}
}


//deferral.Complete();
}

private static void ShowToast(string firstLine, string secondLine)
{
var toastXmlContent =
ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

var txtNodes = toastXmlContent.GetElementsByTagName("text");
txtNodes[0].AppendChild(toastXmlContent.CreateTextNode(firstLine));
txtNodes[1].AppendChild(toastXmlContent.CreateTextNode(secondLine));

var toast = new ToastNotification(toastXmlContent);
var toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.Show(toast);

Debug.WriteLine("Toast: {0} {1}", firstLine, secondLine);
}

}

最佳答案

查看您的代码后,看来您的代码是正确的。

为了触发地理围栏后台任务以显示 toast 信息,请确保以下事项:

1) 请确保您已在 Package.appxmanifest 中完成注册BackgroundTask所需的所有配置,例如您已设置正确的 EntryPoint 并添加了“Location”能力。

详细信息,您可以尝试将您的Package.appxmanifest与官方示例Geolocation进行比较的 Package.appxmanifest。另请检查:Create and register a background taskDeclare background tasks in the application manifest .

2) 请确保您知道如何在模拟器中手动设置位置以模拟手机离开或进入地理围栏位置。有关如何在模拟器中设置位置的更多信息,请查看以下文章: https://msdn.microsoft.com/library/windows/apps/dn629629.aspx#location_and_driving .

3) 请确保您在模拟器中的第二个位置距离您第一次定义的地理围栏不是很远,因为模拟器的行为就像真实设备一样,并且设备预计不会突然从纽约移动到西雅图。否则BackgroundTask不会立即被触发。

4) 地理围栏后台任务的启动频率不能超过每 2 分钟一次。如果您在后台测试地理围栏,模拟器能够自动启动后台任务。但接下来的后续后台任务,则需要等待2分钟以上。

此外,我建议您引用以下有关如何使用 Windows Phone 模拟器来测试具有地理围栏的应用程序的文章: https://blogs.windows.com/buildingapps/2014/05/28/using-the-windows-phone-emulator-for-testing-apps-with-geofencing/ .

谢谢。

关于c# - 后台任务地理围栏触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36161960/

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