android - ProgressDialog with progress and secondProgress -
i'm using asynctask in app downloading several pictures. asynctask implemented in separate class, , use interface update ui thread. progressdialog shown while task executed , progress updated.
far can either show current download progress, or number of pictures allready downloaded , both @ same time.
here asynctask class :
public class downloadpicturestask extends asynctask<string ,integer , boolean> { /** * interface updating ui thread methods */ public downloadpicturesresponse handler = null; private activity callingactivity = null; private string message; public downloadpicturestask(activity activity){ attach(activity); } public void attach(activity activity){ this.callingactivity = activity; } public void detach(){ this.callingactivity = null; } @override protected void onpreexecute() { handler.picturespreexecute(); } /** * each picture, store downloaded file in historic folder <br/> * ff pictures downloaded, return true <br/> * if fail or cancel return false. * @param urls list of picture names * @return boolean */ @override protected boolean doinbackground(string... urls) { inputstream input; outputstream output; httpurlconnection connection; try{ (int = 0 ; < urls.length ;i++ ) { string url = urls[i]; url newurl = new url(config.url_pictures + url); connection = (httpurlconnection) newurl.openconnection(); connection.connect(); if (connection.getresponsecode() != httpurlconnection.http_ok) { message = connection.getresponsecode() + " : " + connection.getresponsemessage(); return boolean.false; } int filelength = connection.getcontentlength(); input = connection.getinputstream(); file historicfolder = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures), config.historic_directory); if (!historicfolder.exists()) { boolean mkdir = historicfolder.mkdir(); if (!mkdir) { message = "impossible de créer le dossier historique."; return boolean.false; } } output = new fileoutputstream(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + "/" + config.historic_directory + "/" + url); byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { if (iscancelled()) { input.close(); message = "téléchargement annulé"; return boolean.false; } total += count; /* here publish progress */ if (filelength > 0) publishprogress((int) (total * 100 / filelength),i); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } }catch (exception e){ e.printstacktrace(); log.e(config.log_error, "measurepicturetask", e); message = e.tostring(); return boolean.false; } message = "téléchargement terminé"; return boolean.true; } /** * calling handler concerned method * @param values integer value of progress */ @override protected void onprogressupdate(integer... values) { handler.picturesonprogressupdate(values[0],values[1]); } /** * calling handler concerned method * @param succeed boolean */ @override protected void onpostexecute(boolean succeed) { if(callingactivity != null) handler.picturesonpostexecute(succeed,message); else { log.e(config.log_error,"downloadpicture task : calling activity has been lost."); } }
and here calling activity (only asynctask part) :
@override public void picturespreexecute() { pdial = new progressdialog(this); pdial.setmessage("chargement des photos"); pdial.setprogressstyle(progressdialog.style_horizontal); pdial.setmax(pictures.size()); pdial.setcancelable(true); pdial.setoncancellistener(new dialoginterface.oncancellistener() { @override public void oncancel(dialoginterface dialog) { picturetask.cancel(true); } }); pdial.show(); } @override public void picturesonprogressupdate(int progress,int nbpictures) { pdial.setsecondaryprogress(progress); pdial.setprogress(nbpictures); } @override public void picturesonpostexecute(boolean succeed, string result) { pdial.dismiss(); setrequestedorientation(activityinfo.screen_orientation_sensor); if(succeed) toast.maketext(this,r.string.t_picture_download_succeed,toast.length_short).show(); else toast.maketext(this,result,toast.length_short).show(); }
and kind of display, percent value of actual dowload, , numeric value value allready downloaded pictures :
for need create new class extend progessbar. detail please check http://colintmiller.com/how-to-add-text-over-a-progress-bar-on-android/
Comments
Post a Comment