WP 8.1 App中具有后台任务的多个Geofences的执行问题

时间:2015-07-16 13:30:21

标签: c# windows windows-phone-8 windows-runtime

我是WP 8.1 App开发的新手,我一直在尝试构建一个应用程序,当用户输入地理围栏时,该应用程序会发送Toast通知。当仅设置一个地理围栏时,这很有效。但是当我设置超过1个地理围栏时,有时只在所有地理围栏被激活时才显示祝酒词,有时候在到达第一个目的地后显示所有祝酒词。例如,如果我设置了两个地理围栏,一个在Googleplex,另一个在迪斯尼乐园,只有当我去了Googleplex和迪斯尼乐园时才会显示祝酒词。 (或者当我去第一个目的地时显示两个吐司)。我最终知道如何处理这个问题。有没有办法解决这个问题?

(下面的代码在图像的点击上设置地理围栏)('App.nameofplace'捕获所选地点的名称。'selectedpoint'捕获地点的坐标。)

   private async void SetAlarm_Tapped(object sender, TappedRoutedEventArgs e)
    {
        //This line of code sets up a custom DataModel that need to display details about the geofence that has been set.

        await App.DataModel.AddCurrentAlarmAsync(App.nameofplace,DateTime.Now,selectedPoint.Position.Latitude,
        selectedPoint.Position.Longitude,
        currentPoint.Position.Latitude,currentPoint.Position.Longitude,vicinity);




        // Set fence ID
        string fenceId = App.nameofplace;


        double radius = 300; // in meters

        // Set circular region for geofence
        Geocircle geocircle = new Geocircle(selectedPoint.Position, radius);

        // Remove geofence after the first trigger
        bool singleUse = true;

        // Set monitored states
        MonitoredGeofenceStates monitoredStates =
                        MonitoredGeofenceStates.Entered;


        // Set how long you need to be in geofence for enter event to fire
        TimeSpan dwellTime = TimeSpan.FromSeconds(1.00);

        // Set how long the geofence should be active
        TimeSpan duration = TimeSpan.FromDays(5);

        // Set up the start time of the geofence
        DateTimeOffset startTime = DateTime.Now;

        // Create geofence
        Geofence geofence = new Geofence(fenceId, geocircle, monitoredStates, singleUse, dwellTime, startTime, duration);
        GeofenceMonitor.Current.Geofences.Add(geofence);

        //Register Background task
        await Register(App.nameofplace);
        Frame.Navigate(typeof(MainPage));
               }

注册后台任务的方法

        public async Task Register(string taskName)
    {

        if (!IsTaskRegistered(taskName))
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();
            var builder = new BackgroundTaskBuilder();
            builder.Name = taskName;
            builder.TaskEntryPoint = "WindowsRuntimeComponent1.Bgt";
            builder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));


            builder.Register();




        }
        return;
    }

检查任务是否已注册

     private static bool IsTaskRegistered(string taskName)
    {
        // throw new NotImplementedException();
        var taskRegistered = false;
        var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(task => task.Value.Name == taskName);
        if (entry.Value != null)
        {
            taskRegistered = true;
        }

        return taskRegistered;
    }

后台任务(WinRT组件)

public sealed class Bgt:IBackgroundTask
{
   BackgroundTaskDeferral _deferral = null;


    public void Run(IBackgroundTaskInstance taskInstance)
    {



        ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

        XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
        toastTextElements[0].AppendChild(toastXml.CreateTextNode("You have reached " + taskInstance.Task.Name + "."));
        XmlNodeList toastImageElements = toastXml.GetElementsByTagName("image");
        ((XmlElement)toastImageElements[0]).SetAttribute("src", "ms:appx///Assets/Square71x71Logo.scale-240.png");
        ((XmlElement)toastImageElements[0]).SetAttribute("alt", "Default Graphic");
        IXmlNode toastNode = toastXml.SelectSingleNode("/toast");


        ((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}");
        ToastNotification toast = new ToastNotification(toastXml);

        ToastNotificationManager.CreateToastNotifier().Show(toast);


        taskInstance.Task.Unregister(true);



        _deferral.Complete();

    }




}

}

0 个答案:

没有答案