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.
We can use the DownloadManager
instance to initiate, cancel, and query downloads:
[assembly: UsesPermission(Manifest.Permission.Internet)]
DownloadManager
so that we can work with it:var manager = DownloadManager.FromContext(this);
Enqueue()
method with a DownloadManager.Request
instance:var request = new DownloadManager.Request(Uri.Parse(uri)); long downloadId = manager.Enqueue(request);
DownloadNetwork
flags enumeration:request.SetAllowedNetworkTypes(DownloadNetwork.Wifi);
Remove()
method:var removed = manager.Remove(downloadId);
DownloadManager.Query
to the InvokeQuery()
method:var query = new DownloadManager.Query(); query.SetFilterById(downloadId); ICursor cursor = manager.InvokeQuery(query);
if (cursor.MoveToFirst()) { var statusIndex = cursor.GetColumnIndex( DownloadManager.ColumnStatus); var soFarIndex = cursor.GetColumnIndex( DownloadManager.ColumnBytesDownloadedSoFar); var totalIndex = cursor.GetColumnIndex( DownloadManager.ColumnTotalSizeBytes); int status = (DownloadStatus)cursor.GetInt(statusIndex); double soFar = cursor.GetDouble(soFarIndex); double total = cursor.GetDouble(toitalIndex); string progress = (soFar / total).ToString("0.00 %"); }
If we want to be notified when the download is completed, is canceled, or fails, we register a BroadcastReceiver
instance with the activity or service:
BroadcastReceive
that will handle the download event:private class DownloadReceiver : BroadcastReceiver { public override void OnReceive( Context context, Intent intent) { long downloadId = intent.GetLongExtra( DownloadManager.ExtraDownloadId, 0); } }
DownloadManager instance
as we normally would:var manager = DownloadManager.FromContext(context); var query = new DownloadManager.Query(); query.SetFilterById(downloadId); ICursor cursor = manager.InvokeQuery(query); if (cursor.MoveToFirst()) { // handle the download }
RegisterReceiver()
method:var filter = new IntentFilter( DownloadManager.ActionDownloadComplete); receiver = new DownloadReceiver(); RegisterReceiver(receiver, filter);
if (receiver != null) { UnregisterReceiver(receiver); }
We can also display the Android Downloads app, if our app downloads files that can be accessed by the user directly:
Intent
instance:var intent = new Intent(); intent.SetAction(DownloadManager.ActionViewDownloads); StartActivity(intent);
If we have an app that downloads large files, we can use the DownloadManager
instance. The DownloadManager
instance is useful because it will manage our downloads for us, for example by retrying downloads after the network connection is lost or the device is restarted. It will also broadcast an action when the download is completed.
Once we have the DownloadManager
instance, we can start queuing up downloads using the DownloadManager.Request
type and the Enqueue()
method. The request is constructed with a URI, but can have several additional properties set.
We could set the download to be invisible to the user by passing false
to the SetVisibleInDownloadsUi()
method, or we can specify on which types of network the download can run via the SetAllowedNetworkTypes()
method.
When a download is queued, the Enqueue()
method will return an ID for the download. We can use the ID to find out the progress or state of the download. If we want to query the status of a download, we construct a DownloadManager.Query
instance and set the filter via the SetFilterById()
or SetFilterByStatus()
methods.
Once we have a query, we can get results by invoking the InvokeQuery()
method on the download manager. Similar to querying a ContentProvider()
method, we will receive an ICursor
instance that we can use. With the cursor, we read the progress, status, any errors, and paths associated with a download.
If we want to be notified when a download is complete, we can create a BroadcastReceiver
instance that will respond to the ActionDownloadComplete
broadcast. This broadcast will contain the ID of the download in the extras of the intent, which we can use to update our app.
18.190.159.10