-1

I know how to make a simple downloader for Android. The trick is simple, you make a connection, start getting data and you place the data in the file the same way you receive it and thus you end up downloading the file.

But what if I want to add pause & resume functionality?

Ok, I can understand how many bytes have been written by getting the length of the file and comparing it with the content length from the HttpURLConnection and start writing the byte next to it.

How do I tell the server to send me the bytes that I need rather than sending the bytes from the start ?

Note: I am not looking for pre-existing libraries to assist with this question.

Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 1
    That's not how typical download servers works. Pause and resume functionality requires a special protocol. – Sotirios Delimanolis Aug 14 '13 at 15:58
  • @SotiriosDelimanolis Elaborate please ? :) – An SO User Aug 14 '13 at 15:59
  • you can send in your request the size of your file stored in the phone and then on your server get that size and start sending the next piece of the file. – Tobiel Aug 14 '13 at 16:00
  • 1
    See [here](http://stackoverflow.com/questions/15349296/implement-pause-resume-in-file-downloading). There is a `Range` header you can use to tell the server where to start from, but don't assume all servers can handle it. Here's the wiki entry: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header – Sotirios Delimanolis Aug 14 '13 at 16:01
  • @SotiriosDelimanolis and how do I know that the server does not handle it ? :) – An SO User Aug 14 '13 at 16:06
  • @LittleChild By trying it :p. This will not work with all web sites or servers. – Sotirios Delimanolis Aug 14 '13 at 16:06
  • @SotiriosDelimanolis I already read a few SO posts on this. I just want to let the user know that the download can not be resumed :) – An SO User Aug 14 '13 at 16:07

1 Answers1

1

You may want to look into the DownloadManager class from the Android developer docs.

From the docs, it mentions:

The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.

This DownloadManager class is available from API 9+ (gingerbread and higher).

MiStr
  • 1,193
  • 10
  • 17
  • The idea is to re-invent the wheel to learn more. I am a student :-) – An SO User Aug 14 '13 at 16:05
  • Does the `DownloadManager` provide methods to start and pause? – Sotirios Delimanolis Aug 14 '13 at 16:06
  • Why -1? Stackoverflow is often used as a mechanism to help people avoid re-inventing the wheel. The question did not state that this needs to avoid libraries... Please add this stipulation to your question. Others might benefit from the knowledge of the DownloadHelper class. – MiStr Aug 14 '13 at 16:08
  • @MiStr I did not -1, man !! :-) Gave you a +1 so you are balanced :) – An SO User Aug 14 '13 at 16:08
  • @SotiriosDelimanolis - I am not aware of the ability to start/pause. I believe it was meant to take care of the download(s) with minimal user intervention. – MiStr Aug 14 '13 at 16:10
  • @MiStr -1 is mine because this question doesn't answer _How do I tell the server to send me the bytes that I need rather than sending the bytes from the start ?_ Add it as a comment instead. – Sotirios Delimanolis Aug 14 '13 at 16:12
  • @MiStr You are welcome and I am glad you mentioned this library. I can surly learn a lot from it – An SO User Aug 14 '13 at 16:12
  • Ok fine, no more bickering please. Yes, this did benefit me as an OP. Period. :) Do not delete this answer – An SO User Aug 14 '13 at 16:13
  • 1
    Sorry to not have answered the direct question. Yes, this could have merely been a useful comment, hoping to avoid re-inventing the wheel :) – MiStr Aug 14 '13 at 16:14
  • @MiStr Your post attracted so much attention that even `AutoBots` came to edit my post. :D – An SO User Aug 14 '13 at 16:16