performance - Get time (& network speed) to download a file in AsyncTask - Android -


i'm newbie in android.

can say, how display in android ui, during download progressing state, speed @ file (say xyz.mp4) downloading , remaining time complete download xyz.mp4 file.

i using asynctask download task, , want display speed , time along "progress percentage". i'm using progressdialog in dialogfragment.

solution

class downloadvideofromurl extends asynctask<string, string, string> {     @override     protected void onpreexecute() {         super.onpreexecute();          // pre executing codes (ui part)...         // progressdialog initializations...     }      @override     protected string doinbackground(string... params) {         if(params.length < 2)   return "";          string videourlstr = params[0];         string filename = params[1];          try {             url url = new url(videourlstr);             urlconnection conection = url.openconnection();             conection.connect();              // useful can show 0-100% progress bar             int filesizeinb = conection.getcontentlength();              // download file             inputstream input = new bufferedinputstream(url.openstream(), 8 * 1024); // 8kb buffer             file file = new file(environment.getexternalstoragepublicdirectory(                     environment.directory_downloads), filename);             outputstream output = new fileoutputstream(file);             int buffersizeinb = 1024;             byte bytebuffer[] = new byte[buffersizeinb];             int bytesread;              long bytesininterval = 0;             int timelimit = 500;    // ms.             long timeelapsed = 0;   // ms.             long nlwspeed = 0;      string nlwspeedstr = null;              long availableb = 0;             long remainingbytes = filesizeinb;             long remainingtime = 0; string remainingtimestr = null;              long startingtime = system.currenttimemillis();             while ((bytesread = input.read(bytebuffer)) != -1) {    // wait download bytes...                 // bytesread => bytes red                 output.write(bytebuffer, 0, bytesread);                 availableb += bytesread;                 bytesininterval += bytesread;                 remainingbytes -= bytesread;                  timeelapsed = system.currenttimemillis() - startingtime;                 if(timeelapsed >= timelimit) {                     nlwspeed = bytesininterval*1000 / timeelapsed;  // in bytes per sec                     nlwspeedstr = nlwspeed + " bytes/sec";                      remainingtime = (long)math.ceil( ((double)remainingbytes / nlwspeed) ); // in sec                     remainingtimestr = remainingtime + " seconds remaining";                      // resetting calculating nlwspeed of next time interval                     timeelapsed = 0;                     bytesininterval = 0;                     startingtime = system.currenttimemillis();                   }                 publishprogress(                         "" + availableb,    // == string.valueof(availableb)                          "" + filesizeinb,                         "" + bytesread,     // not using. debugging...                         remainingtimestr,                         nlwspeedstr);             }              output.flush();             output.close();             input.close();         } catch (exception e) {             return "\n download - error: " + e;         }          return "";     }      protected void onprogressupdate(string... progress) {         int availableb = integer.parseint(progress[0]);         int totalb = integer.parseint(progress[1]);         int percent = (availableb *100)/totalb;   // here percentage of download...          string remainingtime = progress[3];         string nlwspeed = progress[4];          if(remainingtime == null || nlwspeed == null) return;          // show details in ui:         // percent, remainingtime, nlwspeed...     }      @override     protected void onpostexecute(string result) {         // code after download completes (ui part)     } } 

have checked trafficstats class? there wealth of information in there.

here example of trafficstats

if looking maximum download/upload speed network interface, wget has been ported android, can use of these answers

here source code of small app measures download speed on edge or 3g detecting network speed , type on android (edge,3g)

you can try following code

private speedinfo calculate(final long downloadtime, final long bytesin) {     speedinfo info=new speedinfo();     //from mil sec     long bytespersecond   = (bytesin / downloadtime) * 1000;     double kilobits = bytespersecond * byte_to_kilobit;     double megabits = kilobits  * kilobit_to_megabit;     info.downspeed = bytespersecond;     info.kilobits = kilobits;     info.megabits = megabits;      return info; }  private static class speedinfo {      public double kilobits = 0;     public double megabits = 0;     public double downspeed = 0;         }   private static final int expected_size_in_bytes = 1048576; //1mb 1024*1024  private static final double edge_threshold = 176.0; private static final double byte_to_kilobit = 0.0078125; private static final double kilobit_to_megabit = 0.0009765625; 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -