*android multi-threaded download Imitation download assistant (improved version)

Source: Internet
Author: User

Let's first declare one thing: multi-threaded download here does not refer to multiple threads downloading a file. Instead, each thread is responsible for a file.

Real multi-threaded hope behind can bring you.

------------- Welcome to the small group of friends who love learning -------------

-------------android AC Group: 230274309-------------

-------------to share it with. Progress together!

You need to--------------

--------------look forward to the arrival of our friends who love to learn--------------

This article is followed by the previous http://blog.csdn.net/u011733020/article/details/47016715 continue to write.

Interested small partners can look at the front.

Interface Effect

2015/7/31 Improvement

2015/7/31 the first to say this improved two points.

1th, the previous said that the project is only suitable for learning, as a commercial, the efficiency is not high, because at that time, click on the pause, click Download continue download time. Suppose the file is larger than the previous download. will be slower, because the Java inputstream skip (long size) Skipping bytes This method does not follow the bytes you want to skip, but skips is often smaller. So it's more time-consuming to iterate until it returns to meet the conditions. For example, a file size of 30M. You have downloaded 20M. You point a pause and then continue to download. Skip this 20M, but you may skip 4096 bytes at a time using the Skip method, which can be very long to skip 20M. This should be well understood.

2nd, the original project, you this time the download is not complete. The next time in the download is to delete the original from the new download. This time change to continue to the last place and then download.

Under The Spit Groove. About the download, I have been looking at the open source of the download for a week, but the level of frustration is limited, the harvest is very small. tend to see the last head short.

This change is simpler and modifies only the Downloadmanager class in the project. Looking at the Run method of the Downloadmanager class,

@Overridepublic void Run () {info.setdownloadstate (state_downloading);//change the download status notifydownloadstatechanged (info) first; File File = new file (Info.getpath ());//Get download file Httpresult httpresult = Null;inputstream stream = null;if (Info.getcurrentsi Ze () = = 0 | | !file.exists () | | File.length ()! = Info.getcurrentsize ()) {//If the file does not exist, or the progress is 0, or the progress and file length do not match, you need to download the Info.setcurrentsize again (0); file.delete ();} Httpresult = Httphelper.download (Info.geturl ()); if (Httpresult = = null| | (stream = Httpresult.getinputstream ()) = = null) {info.setdownloadstate (state_error);//No download content returned. Modified to Error status notifydownloadstatechanged (info);} else {try {Skipbytesfromstream (stream, info.getcurrentsize ());} catch (Exception E1) {e1.printstacktrace ();} FileOutputStream fos = null;try {fos = new FileOutputStream (file, true); int count = -1;byte[] buffer = new Byte[1024];whil E (((count = stream.read (buffer))! =-1) && info.getdownloadstate () = = state_downloading) {//each time the data is read, All need to infer whether it is a download state, assuming it is not, the download needs to be terminated, assuming. The Refresh Progress fos.write (buffer, 0, count); fos.fLush (); Info.setcurrentsize (Info.getcurrentsize () + count); notifydownloadprogressed (info);//Refresh Progress}} catch (Exception e) {info.setdownloadstate (state_error); notifydownloadstatechanged (info); info.setcurrentsize (0); File.delete ();} Finally {ioutils.close (FOS); if (httpresult! = null) {Httpresult.close ();}} Infer if the progress is equal to the total length of the app if (info.getcurrentsize () = = Info.getappsize ()) {

*android multi-threaded download copy download Assistant (improved version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.