async void MainPage_Loaded(object sender, RoutedEventArgs e) { var promise = await BackgroundExecutionManager.RequestAccessAsync(); if (promise == BackgroundAccessStatus.Denied) { MessageDialog warningDialog = new MessageDialog("Background execution is disabled. Please re-enable in the Battery Saver app to allow this app to function", "Background GPS"); await warningDialog.ShowAsync(); } else { var defaultSensor = Windows.Devices.Sensors.Accelerometer.GetDefault(); if (defaultSensor != null) { var deviceUseTrigger = new DeviceUseTrigger(); deviceUseTask = RegisterBackgroundTask("BackgroundGps.Engine.BackgroundGpsTask", "GpsTask", deviceUseTrigger, null); try { DeviceTriggerResult r = await deviceUseTrigger.RequestAsync(defaultSensor.DeviceId); System.Diagnostics.Debug.WriteLine(r); //Allowed } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } } }
/// <summary> /// Starts the sensor background task. /// </summary> /// <param name="deviceId">Device Id for the sensor to be used by the task.</param> /// <param name="e"></param> /// <returns>True if the task is started successfully.</returns> private async Task <bool> StartSensorBackgroundTaskAsync(String deviceId) { bool started = false; // Make sure only 1 task is running. FindAndCancelExistingBackgroundTask(); // Register the background task. var backgroundTaskBuilder = new BackgroundTaskBuilder() { Name = SampleConstants.Scenario1_DeviceUse_TaskName, TaskEntryPoint = SampleConstants.Scenario1_DeviceUse_TaskEntryPoint }; backgroundTaskBuilder.SetTrigger(_deviceUseTrigger); _deviceUseBackgroundTaskRegistration = backgroundTaskBuilder.Register(); // Make sure we're notified when the task completes or if there is an update. _deviceUseBackgroundTaskRegistration.Completed += new BackgroundTaskCompletedEventHandler(OnBackgroundTaskCompleted); try { // Request a DeviceUse task to use the accelerometer. DeviceTriggerResult deviceTriggerResult = await _deviceUseTrigger.RequestAsync(deviceId); switch (deviceTriggerResult) { case DeviceTriggerResult.Allowed: rootPage.NotifyUser("Background task started", NotifyType.StatusMessage); started = true; break; case DeviceTriggerResult.LowBattery: rootPage.NotifyUser("Insufficient battery to run the background task", NotifyType.ErrorMessage); break; case DeviceTriggerResult.DeniedBySystem: // The system can deny a task request if the system-wide DeviceUse task limit is reached. rootPage.NotifyUser("The system has denied the background task request", NotifyType.ErrorMessage); break; default: rootPage.NotifyUser("Could not start the background task: " + deviceTriggerResult, NotifyType.ErrorMessage); break; } } catch (InvalidOperationException) { // If toggling quickly between 'Disable' and 'Enable', the previous task // could still be in the process of cleaning up. rootPage.NotifyUser("A previous background task is still running, please wait for it to exit", NotifyType.ErrorMessage); FindAndCancelExistingBackgroundTask(); } return(started); }
private async Task <bool> StartAccelerometerBackGroundTask(String deviceId) { bool started = false; try { // Request a DeviceUse task to use the accelerometer. DeviceTriggerResult accelerometerTriggerResult = await manualTrigger.RequestAsync(deviceId); #warning background task seems to be run from here switch (accelerometerTriggerResult) { case DeviceTriggerResult.Allowed: Status = "Background task started"; started = true; break; case DeviceTriggerResult.LowBattery: Error = "Insufficient battery to run the background task"; break; case DeviceTriggerResult.DeniedBySystem: // The system can deny a task request if the system-wide DeviceUse task limit is reached. Error = "The system has denied the background task request"; break; default: Error = "Could not start the background task: " + accelerometerTriggerResult; break; } } catch (InvalidOperationException) { // If toggling quickly between 'Disable' and 'Enable', the previous task // could still be in the process of cleaning up. Status = "A previous background task is still running, please wait for it to exit"; FindAndCancelExistingBackgroundTask(); } return(started); }
/// <summary> /// Triggers the background task to update firmware for the device. The update will cancel if it's not completed within 2 minutes. /// /// Before triggering the background task, all UsbDevices that will have their firmware updated must be closed. The background task will open /// the device to update the firmware, but if the app still has it open, the background task will fail. The reason why this happens is because /// when a UsbDevice is created, the corresponding device is opened exclusively (no one else can open this device). /// /// The trigger.RequestAsync() must be started on the UI thread because of the prompt that appears. The caller of UpdateFirmwareForDeviceAsync() /// is responsible for running this method in the UI thread. /// </summary> /// <param name="deviceInformation"></param> /// <returns>An error message</returns> private async Task <String> StartFirmwareForDeviceAsync(DeviceInformation deviceInformation) { DeviceTriggerResult deviceTriggerResult = await firmwareUpdateBackgroundTaskTrigger.RequestAsync(deviceInformation.Id, FirmwareUpdateTaskInformation.ApproximateFirmwareUpdateTime); // Determine if we are allowed to do firmware update String statusMessage = null; switch (deviceTriggerResult) { case DeviceTriggerResult.Allowed: isUpdatingFirmware = true; statusMessage = "Firmware update was allowed"; break; case DeviceTriggerResult.LowBattery: isUpdatingFirmware = false; statusMessage = "Insufficient battery to start firmware update"; break; case DeviceTriggerResult.DeniedByUser: isUpdatingFirmware = false; statusMessage = "User declined the operation"; break; case DeviceTriggerResult.DeniedBySystem: // This can happen if the device metadata is not installed on the system. // The app must be a privileged app isUpdatingFirmware = false; statusMessage = "Firmware update operation was denied by the system"; break; default: isUpdatingFirmware = false; statusMessage = "Failed to initiate firmware update - Unknown Reason"; break; } return(statusMessage); }