I have a UWP Windows 8.1 application which uses BackgroundDownloader to load pictures. I need to be able to download even when application was suspended. The application runs on Windows 8 and Windows 10. In Windows 10 it is possible to disable background tasks (Settings -> Privacy -> Background apps). If my application is prevented from using background tasks, downloads will never be finished.
I tried to use BackgroundExecutionManager.GetAccessStatus() to determine current permissions and switch to HttpClient if the permissions was turned off. I ran into two problems:
- I can get current state only before the download. I didn't find any way to get notification or something from system that settings were changed.
- On Windows 8, calling this method causes the application to crash.
So I have 2 questions:
- Is there any way to use BackgroundDownloader, when the Background Apps setting in Windows 10 is turned off? (I want to notice that the same code in UWP Windows 10 application works even with the the setting turned off).
- Is there any way that works properly on both Windows 8 and 10 to determine background apps setting state and react if it was changed?
I use code like
var downloader = new BackgroundDownloader();
var download = downloader.CreateDownload(source, destinationFile);
await HandleDownloadAsync(download, true);
...
private async Task HandleDownloadAsync(DownloadOperation download, bool start)
{
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
var cts = new CancellationTokenSource();
cts.CancelAfter(120000);
if (start)
{
await download.StartAsync().AsTask(cts.Token, progressCallback);
}
else
{
await download.AttachAsync().AsTask(cts.Token, progressCallback);
}
}