简体   繁体   English

如何使用bytes_downloaded和bytes_total Android / Java衡量下载速度

[英]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. 如何使用两个值1.文件总大小和2.总字节下载来计算文件下载速度。

@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. 因此,现在我有一个文件的两个值bytes_downloaded和bytes_total,如何使用这两个值来计算文件下载速度。 Thanks in Advance. 提前致谢。

Since you already have the bytes_downloaded value, all you need now is the time dimension. 由于您已经具有bytes_downloaded值,因此现在所需的只是时间维度。

bytes_downloaded / seconds taken = speed in bytes per second 

If it took 10 seconds to download 10000 bytes, your download speed will be 如果花费10秒下载10000字节,则您的下载速度将为

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 . 不需要总文件大小来计算速度,但是需要计算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. 对于采样,您决定采用秒,即在1000毫秒后将检查下载的字节值。 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. 现在,您需要运行一个异步任务(该任务将检查下载的字节并在循环中以预定的1000ms间隔执行采样工作),并且应该以计算的速度不断更新(使用发布API)您的UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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