如何测量下载速度使用bytes_downloaded和bytes_total的Android / Java的 [英] How to measure the download speed using bytes_downloaded and bytes_total Android / Java
问题描述
我如何计算使用两个值1.Total文件大小和2.Total字节要下载的文件的下载速度。
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();
}
}
所以现在我有两个值bytes_downloaded和文件的bytes_total,我怎么能使用这个两个值计算文件的下载速度。先谢谢了。
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.
推荐答案
您需要做的抽样即有将已下载的总字节数的静态变量。对于采样你决定去秒即下载后1000毫秒将检查字节的值。现在你需要运行一个异步任务(这将检查你的字节下载和1000毫秒的pre-决定间隔循环中执行的取样工作),并应不断更新(使用发布API)你的用户界面与速度计算出来的。
You need to do sampling i.e. have a static variable which will have total number of bytes downloaded. For sampling you decided to go with sec i.e. 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.
这篇关于如何测量下载速度使用bytes_downloaded和bytes_total的Android / Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!