Hello Friends, A few weeks ago we talk about downloading files bytes by bytes in Xamarin iOS. That post explained how to use NSUrl to download large files, monitor the download’s progress, and a few more interesting things. Today, we will switch from iOS to Android, and see how to properly download files of all sizes with the Xamarin Android Download Manager.

What we will Cover

  • Configuring the download manager
  • Monitoring download progress inside your app
  • Listening to download completion with broadcast receivers

Configuring the Xamarin Download Manager

When you want to download a file in your app, first you need to configure permissions. There are basically 3 permissions to add to your app’s manifest. Two of these permissions are requested at runtime.

After adding these permissions to your manifest, go to your code and request the “READ_EXTERNAL_STORAGE” and “WRITE_EXTERNAL_STORAGE” permissions. To do this, we use Xamarin Essentials (Yeah, this plugin makes life sooo easy 🙂 ). This is how we do it:

Then, we have to tell the download manager which URL to download from, where to save the downloaded file, display the download notification, and how to monitor the download progress in our app. To know more custom properties for the download manager, Check this.

Note: You should save the download id if you are willing to access your download info later from Android.

Note: From Android 13 and above, access to files has been restricted, so the download path should not be the app’s folder else Android will throw an unauthorized access exception. You should use the method above to tell the download manager a path to a directory accessible by your app (Downloads, Pictures…). Later, if you want the download to be in your app’s private folder, you will have to move it when the download completes.

Monitoring The Download Progress In Your App

One of the principal features we want when implementing a download functionality is to let the user visualize the progress of the download. The Xamarin Android Download manager allows us to display a notification with the progress. But this might not be sufficient. You might want to monitor this download progress in your code and inform the user of every download progress.

While reading the Android documentation and going through a lot of examples (Recent and old, Java & Kotlin etc) I noticed that querying the download manager’s database for progress isn’t difficult. The complex part is determining the right time to query this database exactly when the download percentage changes. You can find online examples using ContentObservers and ContentProviders. I experimented with these and they didn’t work for me If you managed to make this work, please share in the comments section.

To know the right time to query the download progress of the Xamarin download manager, we will use Xamarin’s Timer. This API will use Android’s underlying native timer functionalities and perform our queries every n second we configure it.

The download monitor we use in the code above is an object that contains all the logic to query our download’s progress. This it’s implementation.

Listenning to Download Completion With a Broadcast Receiver

You might want to just listen to the download completion and take appropriate actions when this occurs. First, you will need to create a broadcast receiver. Below, you can see how to do just that. Learn more about broadcast receivers here.

Then register your download broadcast receiver as shown below.

Conclusion

Above we have the complete code to configure the Xamarin Download Manager, Monitor Download Progress, and receive download completed broadcasts. You might also link to know how to implement this functionality with Xamarin iOS find it here.

References

https://www.programmersought.com/article/2905971463/

https://docs.microsoft.com/en-us/answers/questions/212514/how-to-check-download-manager-file-downloaded-and.html

Follow me on social media and stay updated

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.