Example #1
0
        }//RequestBackgroundAccess end

        /// <summary>
        /// Finds a previously registered background task for this scenario and cancels it (if present)
        /// </summary>
        private void FindAndCancelExistingBackgroundTask()
        {
            foreach (var backgroundTask in BackgroundTaskRegistration.AllTasks.Values)
            {
                if (SaferConfiguration.GetTaskName() == backgroundTask.Name)
                {
                    ((BackgroundTaskRegistration)backgroundTask).Unregister(true);
                    break;
                }
            }
        }
Example #2
0
        public bool RegisterBackgroundTask()
        {
            FindAndCancelExistingBackgroundTask();

            BackgroundTaskBuilder accelerometerTaskBuilder = new BackgroundTaskBuilder()
            {
                Name           = SaferConfiguration.GetTaskName(),
                TaskEntryPoint = SaferConfiguration.GetTaskEntryPoint(),
            };

            //Trigger set
            accelerometerTaskBuilder.SetTrigger(manualTrigger);

            //Task registered
            saferAccelerometerRegistrationManual = accelerometerTaskBuilder.Register();

            //background task completion event handler
            saferAccelerometerRegistrationManual.Completed += new BackgroundTaskCompletedEventHandler(OnBackgroundTaskCompleted);

            return(Registered = true);
        }
Example #3
0
        private async void GRegister_Click(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 geofenceTaskBuilder = new BackgroundTaskBuilder();

            geofenceTaskBuilder.Name           = SaferConfiguration.GetTaskName();
            geofenceTaskBuilder.TaskEntryPoint = SaferConfiguration.GetTaskEntryPoint();

            // Create a new location trigger
            var trigger = new LocationTrigger(LocationTriggerType.Geofence);

            // Associate the locationi 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
            saferAccelerometerRegistrationGeofence = geofenceTaskBuilder.Register();

            // Associate an event handler with the new background task
            saferAccelerometerRegistrationGeofence.Completed += new BackgroundTaskCompletedEventHandler(OnBackgroundTaskCompleted);

            switch (backgroundAccessStatus)
            {
            case BackgroundAccessStatus.Unspecified:
            case BackgroundAccessStatus.Denied:
                Error = "This application must be added to the lock screen before the background task will run.";
                break;
            }
        }