Using DownloadManager
If we wish to download files or resources but do not need fine control over the actual download, we can hand the task over to the DownloadManager
instance.
How to do it...
We can use the DownloadManager
instance to initiate, cancel, and query downloads:
As with normal HTTP requests, we need to request permission to access the Internet:
[assembly: UsesPermission(Manifest.Permission.Internet)]
Then, we get hold of
DownloadManager
so that we can work with it:var manager = DownloadManager.FromContext(this);
Once we have the manager, we can start downloads using the
Enqueue()
method with aDownloadManager.Request
instance:var request = new DownloadManager.Request(Uri.Parse(uri)); long downloadId = manager.Enqueue(request);
We can also specify whether we only want the download to progress on Wi-Fi, mobile data, or both by using the
DownloadNetwork
flags enumeration:request.SetAllowedNetworkTypes(DownloadNetwork.Wifi);
If we want to cancel a download, we pass the download ID to the...