I am trying to write a simple application that gets updated. For this I need a simple function that can download a file and show the current progress in a ProgressDialog. I know how to do the ProgressDialog, but I’m not sure how to display the current progress and how to download the file in the first place.
I am trying to write a simple application that gets updated. For this I
Share
There are many ways to download files. Following I will post most common ways; it is up to you to decide which method is better for your app.
1. Use
AsyncTaskand show the download progress in a dialogThis method will allow you to execute some background processes and update the UI at the same time (in this case, we’ll update a progress bar).
Imports:
This is an example code:
The
AsyncTaskwill look like this:The method above (
doInBackground) runs always on a background thread. You shouldn’t do any UI tasks there. On the other hand, theonProgressUpdateandonPreExecuterun on the UI thread, so there you can change the progress bar:For this to run, you need the WAKE_LOCK permission.
2. Download from Service
The big question here is: how do I update my activity from a service?. In the next example we are going to use two classes you may not be aware of:
ResultReceiverandIntentService.ResultReceiveris the one that will allow us to update our thread from a service;IntentServiceis a subclass ofServicewhich spawns a thread to do background work from there (you should know that aServiceruns actually in the same thread of your app; when you extendsService, you must manually spawn new threads to run CPU blocking operations).Download service can look like this:
Add the service to your manifest:
And the activity will look like this:
Here is were
ResultReceivercomes to play:2.1 Use Groundy library
Groundy is a library that basically helps you run pieces of code in a background service, and it is based on the
ResultReceiverconcept shown above. This library is deprecated at the moment. This is how the whole code would look like:The activity where you are showing the dialog…
A
GroundyTaskimplementation used by Groundy to download the file and show the progress:And just add this to the manifest:
It couldn’t be easier I think. Just grab the latest jar from Github and you are ready to go. Keep in mind that Groundy‘s main purpose is to make calls to external REST apis in a background service and post results to the UI with easily. If you are doing something like that in your app, it could be really useful.
2.2 Use https://github.com/koush/ion
3. Use
DownloadManagerclass (GingerBreadand newer only)GingerBread brought a new feature,
DownloadManager, which allows you to download files easily and delegate the hard work of handling threads, streams, etc. to the system.First, let’s see a utility method:
Method’s name explains it all. Once you are sure
DownloadManageris available, you can do something like this:Download progress will be showing in the notification bar.
Final thoughts
First and second methods are just the tip of the iceberg. There are lots of things you have to keep in mind if you want your app to be robust. Here is a brief list:
INTERNETandWRITE_EXTERNAL_STORAGE); alsoACCESS_NETWORK_STATEif you want to check internet availability.Unless you need detailed control of the download process, then consider using
DownloadManager(3) because it already handles most of the items listed above.But also consider that your needs may change. For example,
DownloadManagerdoes no response caching. It will blindly download the same big file multiple times. There’s no easy way to fix it after the fact. Where if you start with a basicHttpURLConnection(1, 2), then all you need is to add anHttpResponseCache. So the initial effort of learning the basic, standard tools can be a good investment.