RxJava2 Demo2- Downloading songs/images using Android Download Manager.

Anshul Jain
Feb 21, 2017 · 4 min read

This is the 2nd part of the series of posts aimed at writing about Android demo projects using RxJava2

The first one can be found here.

About the Project:

This project is for downloading items(songs, images etc) in Android using RxJava2. There are, however 2 conditions which I have set for downloading.

Image for post
Image for post

Let’s dive straight into the code now.
Every downloadable item has the following attributes

Creating a stream of Download Requests:

In the above code snippet, a stream is created using Flowable which will emit Downloadable items. While creating an instance of FlowableOnSubscribe, we get an object of FlowableEmitter in the onSubscribe() method, which will be used for emitting items. The Flowable will keep the items in buffer if the number of items emitted by it is greater than the number of items consumed by the subscriber.

Now as soon, as the user clicks on the download button of the item, the item is emitted from the Flowable.

Creating a Subscriber for consuming Download Requests :

In the above code, we create a Subscriber and then subscribe to the Flowable. While creating an instance of Subscriber, we get an object of Subscription class, which is used for requesting items from the Flowable.

We keep a local variable currentDownloadsCount for tracking the number of current downloads. When we get an item in onNext() method of the subscriber, it means that the subscriber has received an item for download. So we increment the value of the variable by 1. And when the download is completed, we decrement the value by 1.

As soon as a download is completed, a new item from the Flowable stream is requested using the Subscription object.
Do not forget to dispose the subscriber when the activity is destroyed.

if (downloadRequestsSubscription != null) {

Creating the Download Percent Stream:

In the above code, a stream is created using Observable instead of Flowable(because we don’t need Backpressure strategy) which will emit Downloadable items. While creating an instance of ObservableOnSubscribe, we get an object of ObservableEmitter in the onSubscribe() method, which will be used for emitting items.

When a download is given to the DownloadManager, it updates the status (successful, failed, in progress etc) and the total bytes downloaded for a given downloadable item in its db. To know the download status and percent, we poll the db every 200 ms to get the information about the downloaded item.

After getting the information about the item, we check the current download percent and the last emitted download percent. If the difference is greater than 5 percent or if the current download is 100 percent, then we emit this item, else we skip it. This saves us from unnecessarily updating the UI, in case no substantial percent change happens between two polls.

Creating an Observer and subscribing to it:

Finally, we create the observer and subscribe to it. While creating an instance of Subscriber, we get an object of Disposable class, which is used to dispose the connection to the Observable, once we are done receiving events.

Now as soon as we receive the item in the onNext() function of the observer, we update the UI.

The updateImageDetails() functions is responsible for updating each and every item (in Viewholder) based on its download status and download percentage.

Finally, we dispose the connection once the activity is destroyed.

if (downloadPercentDisposable != null) {

The project is hosted at Github. Please do star the project on Github if you find it useful. It will help me on working on more projects.

Thanks for reading the article. There is a lot of documentation and posts on RxJava but very few practical examples which makes it really hard to grasp its concept. I hope that through this article, I was able to make the concept understand a bit.

Also, lets get connected on LinkedIn.

For more about programming, follow Mindorks , so you’ll get notified when we write new posts.

Check out all the top articles at blog.mindorks.com


Our community publishes stories worth reading on Android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store