Sampath Pasupunuri
Sampath Pasupunuri

Reputation: 638

keeping track of the progress of file downloads in AsyncTask

I asked a similar question here AsyncTask and Progressbar.

What I am doing is I am scheduling the download of more than two files one by one in the background AsyncTask.. UI thread may need one of those files at any point of time depending upon the user interaction.. And if the file download is not complete when the UI thread asks for it, I need to show the progress of download of that particular file..

I have seen quiet a few answers about showing the progress bar of a particular file download. But my requirement is somewhat different. I need to keep track of the progress of the file downloads occurring in the background.

Upvotes: 1

Views: 1701

Answers (3)

s.d
s.d

Reputation: 29436

You can use DownloadManager but it won't show progress in your Views. Other option is use an ExecutorService and submit custom Runnable tasks. Wrap ExecutorService in a custom class that maintains a map of tasks submitted. Whenever a file is needed you can query this class whether a task is completed or not, and also show a ListView of running tasks.

Upvotes: 0

paulczak
paulczak

Reputation: 96

Each download should be run on its own thread (in case one file is smaller, one server faster etc).

Try extending thread or creating a runnable that is parameterized by the URI or other identifier for the download. Once you have that, you can invoke 2 threads that'll run until complete. If you need to update the UI (progress bar), you will need to implement a handler and send a message from a thread to the handler. e.g.

in the main activity class:

   public static final int UPDATE_PROGRESS_BAR =0; 
   public final Handler uiHander = new Handler(){
         public void handleMessage(Message msg){
            switch(msg.what){
            case UPDATE_PROGRESS_BAR: // Something like this to handle the case of progress bar update
                 int updateAmount = msg.obj;
                 // do something to update prog. bar
                 break;

and then in the thread just send that message, it'll need a reference to the handler

     uiHander.obtainMessage(<activity name>.UPDATE_PROGRESS_BAR,<integer update>).sendToTarget();

This may be the most portable way of doing it as async tasks implementation has changed version to version and may or may not execute both downloads in parallel (which you clearly want)

Upvotes: 1

Ryan
Ryan

Reputation: 2260

As you're using an AsyncTask you have the option to use onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...).

E.g.

 private class DownloadFilesTask extends AsyncTask<File, Integer, Long> {
 protected Long doInBackground(File... file) {
     // This will call onProgressUpdate
     publishProgress((int) ((i / (float) count) * 100));
 }

 // this will be called on the UI thread
 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
 }

If you're not using an AsyncTask you can create a Handler and post messages to the UI thread that way.

But as you mention file downloads some things to consider (from Android docs):-

  • The device might not have enough space for the expansion files, so you should check before beginning the download and warn the user if there's not enough space.
  • File downloads should occur in a background service in order to avoid blocking the user interaction and allow the user to leave your app while the download completes.
  • A variety of errors might occur during the request and download that you must gracefully handle.
  • Network connectivity can change during the download, so you should handle such changes and if interrupted, resume the download when possible.
  • While the download occurs in the background, you should provide a notification that indicates the download progress, notifies the user when it's done, and takes the user back to your application when selected.

Luckily all of the above are covered in a library from Google, which provides a download with notifications of progress (even if you quit your app). You can use it, or modify the source to your own needs. More info here

http://developer.android.com/google/play/expansion-files.html#AboutLibraries

Upvotes: 1

Related Questions