My application is having a requirement like pause or resume a we call(downloading a file). In my application I need to call a set of Urls sequentially or parallelly and I have to get the time taken to finish the call. It has an option to pause and resume while calling a url. How can I achieve it. I tried Async Task but it does not have pause and resume option. If anybody have an idea please help me for the same.
My application is having a requirement like pause or resume a we call(downloading a
Share
You cannot pause/resume one web service call: HTTP protocol simply does not support it. You can try playing with HTTP/1.1 keep-alive connection, but that may be rather painful and I would strongly advise against it. Instead, what you should do is pause between URL calls. That is, make one call, retrieve data, then either go to the next one or pause before going to the next one.
You can simply achieve this with
AsyncTask. In your class declare abooleanvariable to keep run state (pause/run) and anindicatormember variable of typeObject– you’ll need it for notifications.Then in your
doInBackgroundmethod loop over your URL’s calling each one sequentially. When you completed one call, check that variable. If it’s set tofalse(i.e.pausehas been called), then wait until it’s set to true – with something like this:Finally, you will need stop/resume methods:
UPDATE
If you want to stop and resume a file download using HTTP, then there are two things you need to do.
Handle InputStream from the connection manually, reading, say, 1024 bytes at a time. Keep track of how much you’re read in total. After each read check that the “pause” hasn’t been called. If it has, then wait until the “resume” has been called.
When resume has been called, attempt to continue reading the stream. If the stream has been closed by the server end, then re-establish connection and specify the offset using
RangeHTTP header, e.g.Range: bytes=4096-will request that the server resumes transfer from byte 4096 onward of the stream. Note that not all servers support this header. If your particular server does not, then you’re out of luck.The actual pause/resume/check logic would be the same as described above.