public async void Run(IBackgroundTaskInstance taskInstance) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); try { Geolocator geolocator = new Geolocator(); Geoposition position = await geolocator.GetGeopositionAsync(); ////////////////////////////////////////////////////////////////////////// #if DEBUG XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01); XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text"); toastTextElements[0].AppendChild(toastXml.CreateTextNode(position.Coordinate.Point.Position.Latitude + "|" + position.Coordinate.Point.Position.Longitude)); ToastNotification toastNotification = new ToastNotification(toastXml); ToastNotificationManager.CreateToastNotifier().Show(toastNotification); #endif ////////////////////////////////////////////////////////////////////////// ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; LimitedQueue <GPSInfo> queue; if (settings.Values.ContainsKey("Queue") == true) { string queueString = (string)settings.Values["Queue"]; queue = SerializerUtility.Deserialize <LimitedQueue <GPSInfo> >(queueString); } else { queue = new LimitedQueue <GPSInfo>(20); } queue.Push(new GPSInfo() { Latitude = position.Coordinate.Point.Position.Latitude, Longitude = position.Coordinate.Point.Position.Longitude, PositionSource = position.Coordinate.PositionSource.ToString() }); try { string deviceUniqueId = UniqueIdUtility.GetUniqueId(); GPSInfo[] gpsInfoList = queue.Retrive(); string gpsInfoListJson = SerializerUtility.Serialize <GPSInfo[]>(gpsInfoList); KeyValuePair <string, string>[] postData = new KeyValuePair <string, string> [2]; postData[0] = new KeyValuePair <string, string>("deviceUniqueId", deviceUniqueId); postData[1] = new KeyValuePair <string, string>("gpsInfoList", gpsInfoListJson); await HttpUtility.GetHttpResponse(ConfigUtility.GetValue("SendPositionAPI"), postData); queue.Clear(); } finally { string queueString = SerializerUtility.Serialize <LimitedQueue <GPSInfo> >(queue); settings.Values["Queue"] = queueString; } } catch (Exception ex) { } deferral.Complete(); }
private async void CheckBinding() { try { KeyValuePair <string, string>[] postData = new KeyValuePair <string, string> [1]; postData[0] = new KeyValuePair <string, string>("deviceUniqueId", UniqueIdUtility.GetUniqueId()); string owner = await HttpUtility.GetHttpResponse(ConfigUtility.GetValue("GetDeviceOwnerAPI"), postData); if (owner == "") { BackgroundTaskUtility.UnregisterBackgroundTask("MyTask"); ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; if (settings.Values.ContainsKey("Queue") == true) { settings.Values.Remove("Queue"); } } else { BackgroundTaskUtility.RegisterBackgroundTask("RuntimeComponent.TrackingTask", "MyTask", new TimeTrigger(15, false), null); } await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (owner == "") { ownerTextbox.Text = ""; ownerTextbox.IsEnabled = true; deviceNameTextbox.Text = ""; deviceNameTextbox.IsEnabled = true; bindButton.IsEnabled = true; unbindButton.IsEnabled = false; } else { string[] ownerAndDeviceName = SerializerUtility.Deserialize <string[]>(owner); ownerTextbox.Text = ownerAndDeviceName[0]; ownerTextbox.IsEnabled = false; deviceNameTextbox.Text = ownerAndDeviceName[1]; deviceNameTextbox.IsEnabled = false; bindButton.IsEnabled = false; unbindButton.IsEnabled = true; } bindingGrid.Visibility = Visibility.Visible; }); } catch (Exception ex) { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { bindingGrid.Visibility = Visibility.Collapsed; errorGrid.Visibility = Visibility.Visible; }); } finally { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { progressRing.IsActive = false; progressRing.Visibility = Visibility.Collapsed; }); } }