3

My application needs to constantly connect with remote server to download data. I want to implement a progress bar while the data is downloaded and also show the progress and estimated time left in download.

Can anyone please suggest the best way to do this?

Cristian
  • 198,401
  • 62
  • 356
  • 264
Sapan
  • 1,593
  • 3
  • 24
  • 34

2 Answers2

3

Use an AsyncTask to connect .

In preExecute get the size of the object to download, create progress bar etc. with upperbound size of the object.

in onProgressUpdate calculate how much has been done since last progress update so you can estimate time and update your progress bar with total done so far.

jkhouw1
  • 7,320
  • 3
  • 32
  • 24
2

It depends on type of synchronization... for instance:

  • If your sync process consist of downloading only 1 file that contains all new data (say a JSON file), it's easy. You just have to measure how much of the file have you download (take a look at this question Download a file with Android, and showing the progress in a ProgressDialog)
  • If your sync process involves download different files (say, images, text, etc)... you can implement the progress bar depending how many files are left to download (and hopefully how much they weight).
  • If you download different files but they differ from one sync to another, you can ask first the server how many files the synchronization will use, and the size of each file. Then do same as above.

Ideally you must take into account the size of the files you are receiving + the amount of time you spend using them.

Community
  • 1
  • 1
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • thanks a lot for the quick answer. At the moment I just want to download json file. So, the link you have sent should be enough for me.I will try this out and in case of issues let you know.. – Sapan May 09 '11 at 17:09
  • HI, in my main class i created a progressDialog and then called the execute function of DownLoad File.If there is an error in connecting to the server, I want to update the message in progress bar and then dismiss the progress dialox box. I tried doing this in the catch block in doInBackground function, but i get an error:only the original thread ccan modify the progress dialog? How should i go about this?I am not able to even create a Toast message in DownLoad file class – Sapan May 10 '11 at 05:40
  • That's because the doInBackground method is executed in another thread. In order to do UI stuff inside it, you must use the `runOnUiThread` method. Look for information about it. – Cristian May 10 '11 at 13:12