MultiThreadDownloader icon indicating copy to clipboard operation
MultiThreadDownloader copied to clipboard

如何获取下载速度

Open EvlinLee opened this issue 8 years ago • 4 comments

EvlinLee avatar Feb 06 '17 09:02 EvlinLee

Please write english.

salehmosleh avatar Feb 06 '17 12:02 salehmosleh

@salehmosleh how to get the speed?

EvlinLee avatar Feb 06 '17 12:02 EvlinLee

here is an example , update CallBack Request class , method onProgress in your service:

    private long mLastTime=0;
    private long _last_finished = 0;
    private DecimalFormat DF = new DecimalFormat("0.00")
    @Override
    public void onProgress(long finished, long total, int progress) {
        if (mLastTime == 0) {
            fifn.Status = FileInfo.STATUS_DOWNLOADING;
            mLastTime = System.currentTimeMillis();
            fifn.filesize = total;
            updateDB();
        }
        fifn.progress = progress;
        fifn.downloadPerSize = getDownloadPerSize(finished, total);
        fifn.Status = FileInfo.STATUS_DOWNLOADING;
        long currentTime = System.currentTimeMillis();

        if (currentTime - mLastTime > 800) {
            double downspeedn = (((double)(finished-_last_finished))/1024d)/0.80d;
            String downspeed =  DF.format(downspeedn);
            notify.setContentText("speed is  "+downspeed+"KB/s ...");
            notify.setProgress(100, progress, false);
            mLastTime = currentTime;
            _last_finished = finished;
            updateNotification(FileInfo.STATUS_DOWNLOADING);
            sendBroadCast();
            
        }
    }
   public String getDownloadPerSize(long finished, long total) {
          return (DF.format((float) finished / (1024 * 1024)) + "M/" +
                      DF.format((float) total / (1024 * 1024)) + "M");
  }

salehmosleh avatar Feb 06 '17 15:02 salehmosleh

@salehmosleh thanks.

EvlinLee avatar Feb 25 '17 17:02 EvlinLee