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.AsyncTask
will look like this:doInBackground
) runs always on a background thread. You shouldn't do any UI tasks there. On the other hand, the onProgressUpdate
and onPreExecute
run on the UI thread, so there you can change the progress bar:ResultReceiver
and IntentService
. ResultReceiver
is the one that will allow us to update our thread from a service; IntentService
is a subclass of Service
which spawns a thread to do background work from there (you should know that a Service
runs actually in the same thread of your app; when you extends Service
, you must manually spawn new threads to run CPU blocking operations).ResultReceiver
comes to play:ResultReceiver
concept shown above. This library is deprecated at the moment. This is how the whole code would look like:GroundyTask
implementation used by Groundy to download the file and show the progress:DownloadManager
, which allows you to download files easily and delegate the hard work of handling threads, streams, etc. to the system.DownloadManager
is available, you can do something like this:INTERNET
and WRITE_EXTERNAL_STORAGE
); also ACCESS_NETWORK_STATE
if you want to check internet availability.DownloadManager
(3) because it already handles most of the items listed above.DownloadManager
does 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 basic HttpURLConnection
(1, 2), then all you need is to add an HttpResponseCache
. So the initial effort of learning the basic, standard tools can be a good investment.progressbar
in onProgressUpdate
of Asynctask
and you press back button or finish your activity AsyncTask
looses its track with your UI .And when you go back to your activity, even if download is running in background you will see no update on progressbar. So on OnResume()
try to run a thread like runOnUIThread
with a timer task that updates ur progressbar
with values updating from the AsyncTask
running background.AsyncTask
class to handle creation of progressDialog
at the same context .I think following code will be more reusable.(it can be called from any activity just pass context,target File,dialog message)ProgressDialog
is the way to go. There is the setProgress
method of ProgressDialog
which can be invoked to update the progress level as the file gets downloaded.OnPreExecute()
, publish progress often if you use horizontal style of progress bar of the progress dialog. The remaining part is to optimize the algorithm of doInBackground
.ProgressDialog
as you see in other examples, this one will show progress view from your layout and hide it after completion.