Android xutils Analysis Modify download progress update frequency

Source: Internet
Author: User


Because at the time of the update progress, a bit of card, so think, slow down the interval of progress update. Make it less frequent.

Look directly under the code analysis.

Httphandler.java implementation of the Requestcallbackhandler download progress monitoring


 Private responseinfo<t> Handleresponse (HttpResponse response) throws HttpException, IOException {if (respon        SE = = null) {throw new HttpException ("response is null");        } if (iscancelled ()) return null;        Statusline status = Response.getstatusline ();        int statusCode = Status.getstatuscode ();            if (StatusCode <) {Object result = null;            httpentity entity = response.getentity ();                if (Entity! = null) {isuploading = false;                    if (isdownloadingfile) {autoresume = Autoresume && otherutils.issupportrange (response); String responsefilename = autorename?                    Otherutils.getfilenamefromhttpresponse (response): null;                   Filedownloadhandler Downloadhandler = new Filedownloadhandler (); <span style= "color: #ff0000;" > result = downloadhandler.handleentity (entity, this, Filesavepath, Autoresume, responseFileName); Here I found the progress interface implementation of the entity, we click to see. </span>} else {Stringdownloadhandler Downloadhandler = new Stringdownloadhandler                   (); <span style= "color: #ff0000;" > result = downloadhandler.handleentity (entity, this, CharSet);</span> if (Httputils.shttpcach                    E.isenabled (Requestmethod)) {HttpUtils.sHttpCache.put (Requesturl, (String) result, expiry);        }}} return new Responseinfo<t> (response, (T) result, false); } else if (StatusCode = = 301 | | statusCode = = 302) {if (Httpredirecthandler = = null) {HT            Tpredirecthandler = new Defaulthttpredirecthandler ();            } httprequestbase Request = Httpredirecthandler.getdirectrequest (response);            if (Request! = null) {return this.sendrequest (request); }} else if (StatusCode = = 416) {THrow New HttpException (StatusCode, "Maybe the file has downloaded completely");        } else {throw new HttpException (StatusCode, Status.getreasonphrase ());    } return null; }

Filedownloadhandler.java inside, xutils on the progress of the update did the following operations.

If you are in the progress update state, let it go interval update progress, other states do not set the interval.

public class Filedownloadhandler {public File handleentity (httpentity entity, REQUESTCALLB                             Ackhandler CallbackHandler, String target, Boolean isresume, String responsefilename) throws IOException {if (entity = = NULL | |        Textutils.isempty (target)) {return null;        } File TargetFile = new file (target);            if (!targetfile.exists ()) {File dir = targetfile.getparentfile ();            if (dir.exists () | | dir.mkdirs ()) {targetfile.createnewfile ();        }} long current = 0;        Bufferedinputstream bis = null;        Bufferedoutputstream BOS = NULL;            try {FileOutputStream fileoutputstream = null;                if (isresume) {current = Targetfile.length ();            FileOutputStream = new FileOutputStream (target, true); } else {FILEOUTPUtstream = new FileOutputStream (target);            } long = Entity.getcontentlength () + current;            bis = new Bufferedinputstream (Entity.getcontent ());            BOS = new Bufferedoutputstream (FileOutputStream); if (CallbackHandler! = null &&!callbackhandler.updateprogress (total, Current, true)) {return Targ            Etfile;            } byte[] tmp = new byte[4096];            int Len;                while (len = Bis.read (tmp))! =-1) {bos.write (tmp, 0, Len);                Current + = Len; if (CallbackHandler! = null) {<span style= "color: #ff0000;" > if (!callbackhandler.updateprogress (total, current, false)) {//progress update, updated frequency </span> re                    Turn targetfile;            }}} Bos.flush (); if (CallbackHandler! = null) {<span style= "color: #ff0000;" > Callbackhandler.updateprogresS (total, current, true);//Must be updated </span>}} finally {ioutils.closequietly (bis);        ioutils.closequietly (BOS); } if (Targetfile.exists () &&!            Textutils.isempty (responsefilename)) {File NewFile = new File (Targetfile.getparent (), responsefilename); while (Newfile.exists ()) {newFile = new File (Targetfile.getparent (), System.currenttimemillis () + R            Esponsefilename); } return Targetfile.renameto (NewFile)?        Newfile:targetfile;        } else {return targetfile; }    }}



Finally, we go back to Httphandler.java.

Private long lastupdatetime; @Override public boolean updateprogress (long, Long, Boolean Forceupdateui) {if (callback! = NULL && this.state! = state.cancelled) {if (Forceupdateui) {<span style= "color: #ff0000;" > this.publishprogress (update_loading, total, current);                </span>} else {long currtime = Systemclock.uptimemillis ();                   if (Currtime-lastupdatetime >= callback.getrate ()) {lastupdatetime = Currtime; <span style= "color: #ff0000;" > this.publishprogress (update_loading, total, current) </span><span style= "Color:rgb (255, 0, 0); Font-family:arial, Helvetica, Sans-serif; >//Interval time to update progress </span><span style= "color: #ff0000;"    ></span>}}} return this.state! = state.cancelled; }


Finally, where to set it up. Interval time.

To Requestcallback.java



private static final int default_rate = 1000;


private static final int min_rate = 200;

You can modify this here. Complete

Public final int getrate () {
if (Rate < min_rate) {
return min_rate;
}
return rate;
}

or by going to Setrate (2000) in the callback.


Android xutils Analysis Modify download progress update frequency

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.