/// <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 PC Settings. BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); // Regardless of the answer, register the background task. If the user later adds this application // to the lock screen, the background task will be ready to run. // Create a new background task builder BackgroundTaskBuilder visitTaskBuilder = new BackgroundTaskBuilder(); visitTaskBuilder.Name = SampleBackgroundTaskName; visitTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint; // 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(registered: true); switch (backgroundAccessStatus) { case BackgroundAccessStatus.AlwaysAllowed: case BackgroundAccessStatus.AllowedSubjectToSystemPolicy: // BackgroundTask is allowed _rootPage.NotifyUser("Geofence background task registered.", NotifyType.StatusMessage); // Need to request access to location // This must be done with the background task registeration // because the background task cannot display UI. RequestLocationAccess(); break; default: _rootPage.NotifyUser("Background tasks may be disabled for this app", NotifyType.ErrorMessage); break; } }
/// <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(); }