Android Multi-threaded breakpoint continued download

Source: Internet
Author: User

Recently in the study of download file breakpoint continued, mainly to be able to record the download location, quit the application the next time you can also start downloading from the current location, code for multi-threaded + database, code can run normally, but I encountered a few problems in the process of development, some did not find solutions, share out, I hope some great God can point out:

1. Use HttpURLConnection to get file size, speed in 4.x mobile phone very slow, but found a lot of Chinese sites on the code is basically not resolved, and then Google, found to add Conn.setrequestmethod ("HEAD") You can, get the Baotou, so Conn.disconnect () Close the connection can be much faster


2. The same use httpurlconnection download, the user pauses the download or the download completes the Conn.disconnect () The call sometimes may spend more than 10 seconds, this looked for a long time, 4.X bug, the solution has, but is very troublesome, Google will be able to find, I also refer to the other software breakpoints continue to pass, found that there is also this problem, do not know if there is a convenient solution, hope the big God answer


3.RandomAccessFile Multi-threaded low-efficiency problem, see online said in low-profile mobile phone randomaccessfile inefficient, network support 3m/s download, but read and write speed of only 1m/s, then no matter how fast speed, download rate is 1m/s, I don't know if I can find an alternative.

4. Very occasionally, download a file over 100M, sometimes inexplicably downloaded to more than 90 m, download thread httpurlconnection automatically closed the link, but I just one or two times, I do not know is not the problem of my code, estimate it?

Code I post it, the subject is a class, attach a database class, mainly the interaction between threads, there is a start thread, the main function is when the database does not have the download file information when to create, if there is, go to the database to get, and start a number of download threads to start downloading files When the user pauses, the stop thread is started to end the start thread (if no execution is completed) and the download thread, the 2nd problem occurs, and when the download thread is closed, the 4.x phone shuts down the HTTP connection in a surprisingly slow
Randomaccessfile class

Package Com.android.libcore.download;import Android.os.handler;import Android.os.message;import Com.android.libcore.utils.fileutils;import Java.io.file;import Java.io.inputstream;import Java.io.randomaccessfile;import Java.lang.ref.weakreference;import Java.net.httpurlconnection;import Java.net.URL Import Java.util.arraylist;import java.util.hashmap;/** * Description: Single file download, support breakpoint Continuation * * @author ZZP ([email  Protected]) * @since 2015-08-05 */public class Randomaccessfile {/** download status, getting file size */public static final int state    _getsize = 1;    /** download status, start download */public static final int state_starting = 2;    /** download status, stopping */public static final int state_stoping = 3;    /** download status, stop successful */public static final int state_stoped = 4;    /** download Status, Download complete */public static final int state_finish = 5;    /** the current file download status, default to stop the success, that is, download completed, and ready to start downloading * * * private static int currentstate = state_stoped;    /** the number of threads open for downloading a file */private final int thread_num = 4; /** Download all thread information for a file */priVate arraylist<downloadinfo> infos;    /** Start Download thread */private thread startdownloadthread;    /** end download thread to detect the completion of the download thread, update the status */private Thread stopdownloadthread;    /** Download the thread queue for a file */private arraylist<downloadthread> threads;    /** update download information, update interface thread */private Updatethread updatethread;    /** Database Operation Object */private Downloaddbhelper helper;    /** the URL of the file download */private String URL;    /** the file download path, default to the SD card file directory */private String Path = Fileutils.getexternalstoragefilepath ();    /** the file size of the file download */private long fileSize = 0;    /** completion of the file download */private long completesize = 0;    /** Notice Update Progress handler */private Progresschangehandler Progresschangehandler;    /** File Download Progress update */private Idownloadprogresschangedlistener listener;    Download status of the/** file */private Boolean downloadstate = false;    /** File Download Complete */private Boolean isdownloadfinish = false; /** * @param url file Download URL * @param filename name */public filedownloadmanager (string url, string filENAME) {This (URL, fileName, null); /** * @param url file Download URL * @param filename * @param path file download path, no file name required */Public filedownload        Manager (string URL, string fileName, String path) {this.url = URL;        if (path = null) This.path = path;        if (!this.path.substring (This.path.length ()-1). Equals ("/")) {This.path + = "/";        } This.path + = FileName;        Helper = new Downloaddbhelper ();        Infos = new arraylist<> ();        Threads = new arraylist<> ();        Progresschangehandler = new Progresschangehandler (this);    Checkfilefinish (); /** * Detects if the file has been downloaded/public boolean checkfilefinish () {if (Isdownloadfinish | | isfiledownloadfini            SH (helper.getinfo (URL))) {isdownloadfinish = true;            Progresschangehandler.sendemptymessage (State_finish);        return true;    } return false; }/** * Open download */public void Start ({if (Checkfilefinish ()) {return;        } if (downloadstate) {return;        } if (CurrentState = = state_stoping) {return;        } DownloadState = true;    Open Download Task startdownload (helper.getinfo (URL));        }/** * Stop download */public void Stop () {downloadstate = false; To stop updating the interface thread, make sure that there is at most one update thread executing if (updatethread! = null && updatethread.isalive ()) UPDATETHREAD.C        Anrun = false;        if (Checkfilefinish ()) {return;        } if (CurrentState = = state_stoping) {return;        } if (CurrentState = = state_stoped) {return;    } stopdownload (); } private void Startdownload (final arraylist
Poke me download github entire project download, the younger brother writes an Android frame, inside also has the related code


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Multi-threaded breakpoint continued download

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.