简体   繁体   中英

How to measure the download speed using bytes_downloaded and bytes_total Android / Java

How can I calculate the file download speed using two values 1.Total file size and 2.Total Bytes being downloaded.

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.start_ed) {
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(durl));
            request.setTitle("File Download");
            request.setDescription("file is being download");

            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            String nof = URLUtil.guessFileName(durl, null,
                    MimeTypeMap.getFileExtensionFromUrl(durl));

            request.setDestinationInExternalPublicDir(
                    Environment.DIRECTORY_DOWNLOADS, nof);

            final DownloadManager downloadManager = (DownloadManager) MainActivity.this
                    .getSystemService(Context.DOWNLOAD_SERVICE);
            // long id = downloadManager.enqueue(request); //This is to get the
            // id of the download.
            final long download_id = downloadManager.enqueue(request);

            final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
            final TextView current_tvm = (TextView) findViewById(R.id.current_tv);
            final TextView totl_tv = (TextView) findViewById(R.id.totalsize);
            new Thread(new Runnable() {

                @Override
                public void run() {

                    boolean downloading = true;

                    while (downloading) {

                        DownloadManager.Query q = new DownloadManager.Query();
                        q.setFilterById(download_id);

                        Cursor cursor = downloadManager.query(q);
                        cursor.moveToFirst();
                        final int bytes_downloaded = cursor.getInt(cursor
                                .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        final int bytes_total = cursor.getInt(cursor
                                .getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                        if (cursor.getInt(cursor
                                .getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                            downloading = false;
                        }
                        final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // totl_tv.setText(bytes_total);
                                // current_tvm.setText(bytes_downloaded);
                                String i = android.text.format.Formatter
                                        .formatFileSize(MainActivity.this,
                                                bytes_downloaded);

                                System.out.println(i);
                                current_tvm.setText(i);
                                mProgressBar.setProgress((int) dl_progress);

                            }
                        });

                        // Log.d(Constants.MAIN_VIEW_ACTIVITY,
                        // statusMessage(cursor));
                        cursor.close();
                    }

                }
            }).start();

        }
    }

So now I have two values bytes_downloaded and bytes_total of a file, how can I calculate file download speed using this two values. Thanks in Advance.

Since you already have the bytes_downloaded value, all you need now is the time dimension.

bytes_downloaded / seconds taken = speed in bytes per second 

If it took 10 seconds to download 10000 bytes, your download speed will be

10000 / 10 = 1000 bps = 8 kbps

The total file size is not required to calculate the speed, however, it is required to calculate the estimated time to completion .

You need to do sampling ie have a static variable which will have total number of bytes downloaded. For sampling you decided to go with sec ie the value of bytes downloaded will be checked after 1000ms. Now you need to run an asychronous task(which will check your bytes downloaded and perform your sampling work within a loop with a pre-decided interval of 1000ms) and it should keep updating(using publish API) your UI with the speed calculated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM