/// <summary> /// This is the click handler for the 'Register' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RegisterBackgroundTask(object sender, RoutedEventArgs e) { // Register the background task without checking whether the user has enabled background execution. // If it's disabled, and then the user later enables background execution, the background task will be ready to run. // Create a new background task builder BackgroundTaskBuilder geofenceTaskBuilder = new BackgroundTaskBuilder(); geofenceTaskBuilder.Name = BackgroundTaskName; geofenceTaskBuilder.TaskEntryPoint = BackgroundTaskEntryPoint; // Create a new location trigger var trigger = new LocationTrigger(LocationTriggerType.Geofence); // Associate the location trigger with the background task builder geofenceTaskBuilder.SetTrigger(trigger); // If it is important that there is user presence and/or // internet connection when OnCompleted is called // the following could be called before calling Register() // SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent | SystemConditionType.InternetAvailable); // geofenceTaskBuilder.AddCondition(condition); // Register the background task _geofenceTask = geofenceTaskBuilder.Register(); // Associate an event handler with the new background task _geofenceTask.Completed += OnCompleted; UpdateButtonStates(); MainPage.CheckBackgroundAndRequestLocationAccess(); }
/// <summary> /// This is the click handler for the 'Register' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RegisterBackgroundTask(object sender, RoutedEventArgs e) { // Register the background task without checking whether the user has enabled background execution. // If it's disabled, and then the user later enables background execution, the background task will be ready to run. // Create a new background task builder BackgroundTaskBuilder geolocTaskBuilder = new BackgroundTaskBuilder(); geolocTaskBuilder.Name = BackgroundTaskName; geolocTaskBuilder.TaskEntryPoint = BackgroundTaskEntryPoint; // Create a new timer triggering at a 15 minute interval var trigger = new TimeTrigger(15, false); // Associate the timer trigger with the background task builder geolocTaskBuilder.SetTrigger(trigger); // Register the background task _geolocTask = geolocTaskBuilder.Register(); // Associate an event handler with the new background task _geolocTask.Completed += OnCompleted; UpdateButtonStates(); MainPage.CheckBackgroundAndRequestLocationAccess(); }
/// <summary> /// This is the click handler for the 'Register' button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async private void RegisterBackgroundTask(object sender, RoutedEventArgs e) { // Get permission for a background task from the user. If the user has already answered once, // this does nothing and the user must manually update their preference via Settings. BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); // Regardless of the answer, register the background task. If the user later enables background // execution, the background task will be ready to run. // Create a new background task builder BackgroundTaskBuilder visitTaskBuilder = new BackgroundTaskBuilder(); visitTaskBuilder.Name = BackgroundTaskName; visitTaskBuilder.TaskEntryPoint = BackgroundTaskEntryPoint; // Create a new visit trigger var trigger = new GeovisitTrigger(); // Set the desired monitoring scope. // For higher granularity such as venue/building level changes, choose venue. // For lower granularity more or less in the range of zipcode level changes, choose city. // Choosing Venue here as an example. trigger.MonitoringScope = VisitMonitoringScope.Venue; // Associate the trigger with the background task builder visitTaskBuilder.SetTrigger(trigger); // If it is important that there is user presence and/or // internet connection when OnCompleted is called // the following could be called before calling Register() // SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent | SystemConditionType.InternetAvailable); // visitTaskBuilder.AddCondition(condition); // Register the background task _visitTask = visitTaskBuilder.Register(); // Associate an event handler with the new background task _visitTask.Completed += OnCompleted; UpdateButtonStates(); MainPage.CheckBackgroundAndRequestLocationAccess(); }