0

I haven't started coding yet. I'm trying to figure out which libraries that is out there and what the API can offer first.

I need to download a lot of resources before starting my app. Basically I'm going to have a "SplashSreen" and a progressbar that shows which file being downloaded and the progressbar for the entire progress of downloading the x-number of resources.

After looking through some discussions and tutorials on the subject, it seems to me that AsyncTask will do the job. But it's only possible to show the current progress for the current "object" being downloaded, and not for the entire queue.

Could anyone help me out here, give me some tutorials or something to dig into?

TBH; I'm not even sure if AsyncTask is what I'm looking for in this scenario. Basically I need something that can download resources, put in a queue, and let me know the progress of the entire queue, and which resource (name of it) being download at a certain time.

Thanks!

RonU
  • 5,525
  • 3
  • 16
  • 13
  • How many 'resources' need to be downloaded? What is the estimated size of (totla bytes) of all resources? – Squonk Aug 06 '12 at 10:28
  • @Squonk: a lot, a couple of gigabytes :) –  Aug 06 '12 at 10:32
  • In that case an `AsyncTask` isn't necessarily what you want unless the users have lightning-speed network connections. The answer from Pork to use an `IntentService` is more suited. I'm also slightly concerned that users will have to download that amount of data *before* being able to use your app - I hope they're patient and the wait is worth it. – Squonk Aug 06 '12 at 10:38
  • We use a fast wifi connection. I think I'll try the example provided from CFlex because he gave me some code to work on. I've never made an IntentService. Maybe I'll go over to that solution as a second try. –  Aug 06 '12 at 10:41
  • Sure - give it a try, it will give you a feel for how it will work. Be aware though that in a "splash screen" scenario, if the user receives a phone call, for example, things will get messy (i.e., fail big time) unless you code it right. Good luck. – Squonk Aug 06 '12 at 10:47
  • Forgot to say that this is for a Tablet. Galaxy Tab 2 10.1. The customer/user will have these tablets for this app only. It will be inited/synced once a day, so the time to do this process is not a problem. –  Aug 06 '12 at 10:52
  • OK. Get a feel for the download process with `AsyncTask` but I'd recommend looking into `AlarmManager` to schedule daily downloads with an `IntentService` and using a `Notification` for download progress/success etc. It will work even if the device is in 'sleep' mode if the `AlarmManager` event uses one of the 'wake' modes. – Squonk Aug 06 '12 at 11:38

5 Answers5

1

Of course an AsyncTask can do the job. Just put all your files in a asyncTask which display the progress.

class InitTask extends AsyncTask<Void, Integer, Boolean> {
    private List<String> mNbFiles;

    public InitTask(List<String> filesURL)
    {
        super();
        mFilesURL = filesURL;
    }

    @Override
    protected void onPreExecute() {
        //show and init your progress bar here...
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {

        for(int i = 0 ; i < mNbFiles.size() ; ++i){
            //download one file ...
            publishProgress(i);
        }
    }
}

I hope this will help you. Comment if you have any question.

AMerle
  • 4,354
  • 1
  • 28
  • 43
1

AsyncTask isn't what you should be using for long running downloads. AsyncTasks are really about the UI, and it's important to know that the user navigating away or even rotating their screen will kill the download.

You should use a service to perform the download, so put all of your download code into an IntentService and update some kind of persistant storage to indicate progress (a couple variables in shared preferences would do fine (on to indicate current state, the other to indicate progress)).

You can then use your activity to fire up the service, and then monitor the progress of the downloads via the persistant storage or some callback passed via the intent (persistant storage is probably easiest).

Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
  • 1
    It's possible to create an ongoing `Notification` with `ProgressBar` and/or text information from an `IntentService`. Much easier than using persistent storage to monitor progress. – Squonk Aug 06 '12 at 10:40
0

AsyncTask is exactly what you are looking for, you can queue depending on the API an amount of tasks, im going to check the Asynctask implementation if there is a getter for the queue size, but if not you can make a list of asyncTask and update when its finished.

If you queue 15 tasks you can set your progress bar to 15 max and on each onPostExecute increment the progressBar :)

I will check the queue later and edit my answer.

Regards.

Goofyahead
  • 5,874
  • 6
  • 29
  • 33
0

AsyncTask is very much what you're looking for. That's not really true that you can only update for the current Object. What that means is that you can update for the current AsyncTask.

Below is the onProgressUpdate method.

protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    Log.i(logTag, "onProgressUpdate: " + String.valueOf(values[0]));

    View view = getView();
    if(view != null)
        ((ProgressBar)view.findViewById(R.id.progressBar)).setProgress(values[0]);
}

You see it takes any number of integers as the arguments. In your publishProgress method, you could call onProgressUpdate(itemProgress, totalProgress) and then update 2 different progress bars accordingly in onProgressUpdate.

You do all the downloading in doInBackground and each time you want to update the progress bar/bars, call publishProgress which passes your progress ints to onProgressUpdate.

Mike T
  • 4,747
  • 4
  • 32
  • 52
-1

In your AsyncTask doInBackground() method put your code that downloads all your objects and post updates of the progress bar using a handler. Take a look at this example:

http://developer.android.com/reference/android/widget/ProgressBar.html

The following code goes inside doInBackground():

while (mProgressStatus < 100) {
                 mProgressStatus = doWork();

                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         mProgress.setProgress(mProgressStatus);
                     }
                 });
}
jsstp24n5
  • 594
  • 1
  • 6
  • 13